Skip to content
Bill Liao
Go back

Inorder Successor in BST

Edit page

Given the root of a binary search tree and a node p in it, return the in-order successor of that node in the BST. If the given node has no in-order successor in the tree, return null.

The successor of a node p is the node with the smallest key greater than p.val.

Example 1:

Input: root = [2,1,3], p = 1 Output: 2 Explanation: 1’s in-order successor node is 2. Note that both p and the return value is of TreeNode type.

Example 2:

Input: root = [5,3,6,2,4,null,null,1], p = 6 Output: null Explanation: There is no in-order successor of the current node, so the answer is null.

Constraints:

Approach: Binary Search on BST (Optimal Solution)

Algorithm

  1. Initialize ans to null
  2. While root is not null:
    • If root.val > p.val, root could be the successor. Record it as ans and move left (root = root.left) to look for a smaller candidate
    • Otherwise (root.val <= p.val), root cannot be the successor, so move right (root = root.right)
  3. Return ans

Key Insight

The in-order successor of p is the smallest value greater than p.val. As we walk down the tree, every node whose value exceeds p.val is a candidate; the best candidate is the smallest one, which is found by always trying to go left after recording a candidate. This is exactly a binary search for the lower bound of p.val + 1.

Time & Space Complexity

Java Implementation

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class InorderSuccessorInBST {

    public TreeNode inorderSuccessor(TreeNode root, TreeNode p) {
        TreeNode ans = null;
        while (root != null) {
            if (root.val > p.val) {
                ans = root;
                root = root.left;
            } else {
                root = root.right;
            }
        }
        return ans;
    }
}

Example Walkthrough

For root = [2,1,3], p = 1:

  1. root=2, 2 > 1 → ans=2, move left to node 1
  2. root=1, 1 > 1 is false → move right (null)
  3. Return 2

For root = [5,3,6,2,4,null,null,1], p = 6:

  1. root=5, 5 > 6 false → move right to 6
  2. root=6, 6 > 6 false → move right (null)
  3. Return null — 6 is the maximum node in the tree

Alternative Approach: Exploiting Subtree Structure

The successor can also be described in two cases:

  1. If p has a right subtree, the successor is the leftmost node of that right subtree (the smallest key greater than p)
  2. Otherwise, the successor is the lowest ancestor for which p lies in the left subtree

Both formulations lead to the same O(h) walk; the binary-search approach above is the simplest single-pass version.

Key Insights

  1. Smallest Greater Key: The successor is the lower bound — the minimum node strictly greater than p.val
  2. Candidate Tracking: Every node greater than p.val is a candidate; always trying left finds the minimum
  3. No Parent Pointers: The search works with just the root reference and p
  4. No Successor Case: Returns null naturally when p is the maximum node

The binary search approach is the optimal solution, providing O(h) time and O(1) space.


Edit page
Share this post:

Previous Post
Convert Sorted Array to Binary Search Tree
Next Post
Kth Smallest Element in a BST