Inorder Successor in BST
A medium BST interview guide. Task statement, worked examples, intuition, and step-by-step solutions.
- Topic
- BST
- Sheets
- 2
- Core for
- 2 roles
The problem
Given the root of a binary search tree, find the inorder successor of a given node p. The inorder successor is the node with the smallest key greater than p.val.
Example 1
- Input
- root=[2,1,3], p=1
- Output
- 2
- Why
- Inorder traversal is 1,2,3. The successor of 1 is 2.
Example 2
- Input
- root=[5,3,6,2,4,null,null,1], p=3
- Output
- 4
- Why
- Inorder traversal is 1,2,3,4,5,6. The successor of 3 is 4.
Example 3
- Input
- root=[5,3,6,2,4,null,null,1], p=6
- Output
- null
- Why
- Node 6 is the largest, so it has no inorder successor.
Constraints
- The number of nodes is in the range [2, 104].
- -109 <= Node.val <= 109
- All values are unique.
- p exists in the tree.
How to think about it
Updated 2026-09-09The successor is either the minimum node in p's right subtree, or the lowest ancestor of p whose left child contains p. In a BST without parent pointers, you do not even need to branch on whether p has a right child: binary search from the root naturally keeps track of the deepest node from which you turned left.
Approaches, worst first
Full inorder traversal search
time O(n) · space O(h)
Perform an inorder traversal while remembering the previously visited node. When the previous node equals p, the current node is the successor. Correct, but inspects up to all n nodes regardless of tree structure.
BST search with candidate trackingWrite this one
time O(h) · space O(1)
Start from root with `successor = null`. If `p.val < root.val`, root is a potential successor; record `successor = root` and move left to search for a closer one. If `p.val >= root.val`, move right without updating successor. Stop when root is null.
Where people lose marks · 2
- Updating successor when moving right. When `p.val >= root.val`, root is smaller than or equal to p, so it cannot serve as a successor.
- Missing the null successor case: when p is the maximum value in the tree, no successor exists and the function must safely return null.
The theory behind it
BST — the ground this problem stands on. All BST problems
What BST is
A binary search tree is a binary tree that enforces a strict ordering rule at every node. Every value stored in a node's left branch must be smaller than the node itself, and every value in its right branch must be larger. Because this rule holds true everywhere down the tree, searching for a value does not require checking every branch. At each step, a single comparison lets you discard an entire half of the remaining nodes.
When to reach for it
Reach for a binary search tree when problems mention sorted tree structures, finding the kth smallest element, checking tree validity, or searching within a range of values. Signals include queries for the next greater element in a dynamic set, finding the lowest common ancestor in a sorted tree, or converting sorted arrays into height-balanced trees. When data must remain sorted while supporting continuous insertions and lookups, BST properties are the target.
How the pattern works
Use the ordering property to prune entire subtrees. When looking for a key, move left if the target is smaller than the current node, or move right if it is larger, stopping when values match or when reaching a null link. For validating a tree, do not merely compare a node to its immediate children; pass valid lower and upper value bounds down through recursive calls. Walking a valid binary search tree in-order visits all values in strictly increasing numerical order.
More BST problems
Track in your role's order
Pick your target role and all 370 problems resequence to what that interview actually asks.
Start freeProblem set and role mapping as of .