Inorder predecessor
Inorder predecessor is a medium trees & bst problem solved with the miscellaneous bst questions pattern.
The best approach, optimal (walk down with a candidate), runs in O(h) time and O(1) space.
Below are 2 approaches in Java, from inorder list up.
Problem
Given a BST and a value key, return the inorder predecessor of key: the node with the largest value smaller than key, or null if there is none.
Examples
Example 1
- Input
root = [5, 3, 6, 2, 4, null, null, 1], key = 4- Output
3
Example 2
- Input
same tree, key = 1- Output
null- Why
- 1 is the smallest value.
Constraints
- The tree has
1to10^4nodes; unique values.
Animated walkthrough
A narrated, step-by-step animation that builds the solution from the idea up. Press play, or step through it at your own pace.
Concepts first, then the problem and every approach, step by step.
Space to play or pause · ← → to jump a step · click or drag the bar to seek
Solutions
Try it yourself first. Then compare: each approach lists its idea, the steps, its time and space, and the Java code.
| Approach | Time | Space |
|---|---|---|
| Inorder list | O(n) | O(n) |
| Optimal (walk down with a candidate) | O(h) | O(1) |
1Inorder list
O(n)O(n)Collect the inorder traversal and return the node just before the one with value key.
- Inorder list; find key; return the previous node.
class Solution {
public TreeNode inorderPredecessor(TreeNode root, int key) {
List<TreeNode> nodes = new ArrayList<>();
inorder(root, nodes);
for (int i = 1; i < nodes.size(); i++) if (nodes.get(i).val == key) return nodes.get(i - 1);
return null;
}
private void inorder(TreeNode n, List<TreeNode> out) {
if (n == null) return;
inorder(n.left, out);
out.add(n);
inorder(n.right, out);
}
}2Optimal (walk down with a candidate)
O(h)O(1)The predecessor is the largest value smaller than key. From the root: if node.val < key, remember it and go right to look for a larger one; otherwise go left.
- pred = null; cur = root.
- If cur.val < key: pred = cur; cur = cur.right. Else cur = cur.left.
class Solution {
public TreeNode inorderPredecessor(TreeNode root, int key) {
TreeNode pred = null, cur = root;
while (cur != null) {
if (cur.val < key) { pred = cur; cur = cur.right; }
else cur = cur.left;
}
return pred;
}
}Edge cases to test
- key is the minimum (no predecessor)
- key has a left subtree
Hints
Hint 1
Mirror of the successor: going right records a candidate.
FAQ
What is the best time complexity for Inorder predecessor?
Optimal (walk down with a candidate) runs in O(h) time and O(1) extra space.
Which pattern does Inorder predecessor use?
It is a trees & bst problem that uses the miscellaneous bst questions pattern. Other problems with the same pattern: Inorder Successor in BST, Floor in BST, Ceil in BST.
Is there a brute force solution for Inorder predecessor?
Yes. Inorder list takes O(n) time and O(n) space. Collect the inorder traversal and return the node just before the one with value key.
Which edge cases should I test for Inorder predecessor?
key is the minimum (no predecessor); key has a left subtree.