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
Visualization
Time & Space Complexity
Need to visit all n nodes during inorder traversal
Store all nodes in array plus recursion stack depth
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