Inorder Successor in BST II - Problem

You're given a node in a Binary Search Tree (BST) and need to find its inorder successor - the next node that would appear in an inorder traversal of the tree.

The inorder successor of a node is the node with the smallest value greater than the current node's value. If no such node exists, return null.

The challenge: You only have access to the given node, not the root of the tree! However, each node has a reference to its parent, which makes this problem solvable through clever tree navigation.

Node Structure:

class Node {
    public int val;
    public Node left;
    public Node right;
    public Node parent;
}

Goal: Navigate the tree structure using parent pointers to find the successor without needing the root reference.

Input & Output

basic_successor.py โ€” Python
$ Input: node = Node(4) with tree structure [2,1,4,null,null,3,6]
โ€บ Output: Node(6)
๐Ÿ’ก Note: Node 4 has a right subtree starting with 6. Since 6 has no left child, 6 is the leftmost node in the right subtree and thus the successor.
no_right_subtree.py โ€” Python
$ Input: node = Node(6) with tree structure [2,1,4,null,null,3,6]
โ€บ Output: null
๐Ÿ’ก Note: Node 6 has no right subtree. Going up the parent chain, 6 is always the right child of its ancestors, so there's no successor.
left_child_case.py โ€” Python
$ Input: node = Node(3) with tree structure [5,3,8,2,4,6,9]
โ€บ Output: Node(4)
๐Ÿ’ก Note: Node 3 has a right subtree. The leftmost node in the right subtree starting from 4 is 4 itself, making it the successor.

Visualization

Tap to expand
BST Inorder Successor Visualization841226101457Case 1: Has right subtreeFind leftmost in right subtreeKey Insight: Leverage BST Structureโ€ข Red node (4) has right subtree โ†’ successor is leftmost node (6) in that subtreeโ€ข If no right subtree โ†’ go up until you find ancestor where you're the left child
Understanding the Visualization
1
Check Right Path
If there's a right subtree, the successor is the leftmost node there
2
Navigate Up
Otherwise, go up until you find an ancestor where you came from the left
3
Return Result
That ancestor is your successor, or null if none exists
Key Takeaway
๐ŸŽฏ Key Insight: BST structure + parent pointers enable O(h) successor finding through smart navigation rather than full traversal

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n)

Need to visit all n nodes during inorder traversal

n
2n
โœ“ Linear Growth
Space Complexity
O(n)

Store all nodes in array plus recursion stack depth

n
2n
โšก Linearithmic Space

Constraints

  • The number of nodes in the tree is in the range [1, 104]
  • -105 โ‰ค Node.val โ‰ค 105
  • All Node.val are unique
  • node is guaranteed to be in the tree
  • Each node has a reference to its parent node
Asked in
Facebook 45 Google 38 Amazon 32 Microsoft 28
42.0K Views
Medium Frequency
~15 min Avg. Time
1.9K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen