Inorder Successor in BST II

Updated on 09 June, 2025
Inorder Successor in BST II header image

Problem Statement

In this problem, you're given a specific node from a binary search tree (BST) and asked to determine its in-order successor. The in-order successor of a node in a BST is defined as the node with the smallest key that is greater than the value of the given node (node.val). If the node does not have an in-order successor in the tree, the function should return null.

This task is unique because you only have access to the node itself and its parent, not to the root of the entire tree. The given node class structure (Node) includes the node's value (val), left child (left), right child (right), and the node's parent (parent).

Examples

Example 1

Input:

tree = [2,1,3], node = 1

Output:

2

Explanation:

1's in-order successor node is 2. Note that both the node and the return value is of Node type.

Example 2

Input:

tree = [5,3,6,2,4,null,null,1], node = 6

Output:

null

Explanation:

There is no in-order successor of the current node, so the answer is null.

Constraints

  • The number of nodes in the tree is in the range [1, 104].
  • -105 <= Node.val <= 105
  • All Nodes will have unique values.

Approach and Intuition

Understanding how the in-order traversal works is key to solving this problem. During an in-order traversal of a BST, nodes are visited in a non-decreasing order of their values. When traversing in this manner, the successor of a node is the next node that gets visited.

Here's an intuition leading towards a solution:

  1. If the node has a right child, the in-order successor is somewhere in the right subtree. To find it:

    • Move to the right child of the node.
    • Then, continually move to the leftmost child until there are no more left children. The node you stop at is the successor.
  2. If the node does not have a right child, the in-order successor would be one of its ancestors, specifically the first parent for which the given node lies in the left subtree. To find this:

    • Move up to the parent of the node.
    • Continue moving up until you find a node that is the left child of its parent. The parent of this node is the in-order successor.
  3. If neither of the above conditions is satisfied (no right child and no parent where the node is a left child), the node has no in-order successor, and you return null.

These steps leverage the properties of BSTs, where all values in a node's right subtree are greater than the node’s value, and all values in the left subtree are less. This allows for the efficient determination of the next higher value, which is what the in-order successor is.

Examples from the problem statement to illustrate the approach:

  • Example 1: For the node with value 1 in a tree of 2, 1, 3; the node has no right child but is the left child of 2. Hence, its in-order successor is 2.
  • Example 2: For the node with value 6, since it has no right child and it is not in the left subtree of any parent, it has no successor and thus returns null.

Solutions

  • C++
  • Java
cpp
class Solution {
public:
    Node* findInorderSuccessor(Node* node) {
        if (node->right) {
            node = node->right;
            while (node->left) node = node->left;
            return node;
        }
        
        while (node->parent && node == node->parent->right) node = node->parent;
        return node->parent;
    }
};

This C++ solution aims to find the inorder successor of a given node in a Binary Search Tree (BST) where nodes have an additional parent pointer. The function findInorderSuccessor operates with the input node pointer node and returns the next node in the inorder traversal of the BST.

  • If the node has a right child, the function navigates to the leftmost node in the right subtree, which is the inorder successor.
  • Otherwise, the function traverses up the tree using the parent pointers until it reaches a node that is the left child of its parent. This parent is the inorder successor.
  • If the given node is the rightmost node in the entire tree, the function eventually finds no successor and returns NULL.

This strategy efficiently finds the successor without the need for a full traversal of the tree, and it operates primarily on the basis of BST properties and the parent pointer, ensuring an O(h) runtime where h is the height of the tree.

java
class Solution {
  public Node getNextInorder(Node node) {
    // Seek right then leftmost node
    if (node.right != null) {
      node = node.right;
      while (node.left != null) node = node.left;
      return node;
    }

    // Find the first left parent
    while (node.parent != null && node == node.parent.right) node = node.parent;
    return node.parent;
  }
}

This Java solution accurately determines the inorder successor of a given node within a binary search tree (BST) that might not have a direct connection to the tree root. The method called getNextInorder achieves this via two main scenarios:

  • If the node has a right child, the solution navigates to this right subtree and continuously moves to the leftmost node until there are no more left children. This node is the inorder successor since it is the next node that would appear in an inorder traversal starting from the given node.

  • If the node lacks a right child, the method traces back through the node's ancestors using the parent pointer until it finds an ancestor of which the given node is in the left subtree (i.e., it seeks the first parent for which the node is on the left side). The first such parent is the inorder successor.

Here are the main points for maintaining clarity when using or modifying this solution:

  • Ensure the given input node Node includes attributes for right, left, and parent.
  • The function handles both scenarios: when the node has a right subtree, and when it doesn't.
  • After identifying the next inorder node, the function appropriately returns the node, or null if an inorder successor does not exist.

This method runs efficiently as it navigates only to necessary portions of the tree relevant to finding the inorder successor, keeping operations close to minimal possible.

Comments

No comments yet.