Trees & BST · Pattern 9 of 12
Inorder and reverse inorder: kth smallest and largest
Inorder (left, node, right) visits a BST in increasing order; reverse inorder (right, node, left) visits it in decreasing order. Count the nodes as you go and stop at the kth.
Watch the lesson
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
When to use it
- Kth smallest or kth largest value in a BST
- Visit a BST from the largest value down (running sums of greater values)
- Stop early instead of sorting everything
The template
// kth largest: right, node, left visits values from the largest down
int remaining, answer;
int kthLargest(TreeNode root, int k) {
remaining = k; answer = -1;
reverseInorder(root);
return answer;
}
void reverseInorder(TreeNode node) {
if (node == null) return;
reverseInorder(node.right);
if (remaining > 0 && --remaining == 0) answer = node.val; // the kth visit
if (remaining > 0) reverseInorder(node.left); // stop once found
}
// kth smallest: iterative inorder, stop at the kth pop
int kthSmallest(TreeNode root, int k) {
Deque<TreeNode> st = new ArrayDeque<>();
TreeNode cur = root;
while (cur != null || !st.isEmpty()) {
while (cur != null) { st.push(cur); cur = cur.left; }
cur = st.pop();
if (--k == 0) return cur.val;
cur = cur.right;
}
return -1; // fewer than k nodes
} # kth largest: right, node, left visits values from the largest down
def kth_largest(root, k):
remaining, answer = k, -1
def visit(node):
nonlocal remaining, answer
if not node:
return
visit(node.right)
if remaining > 0:
remaining -= 1
if remaining == 0:
answer = node.val # the kth visit
if remaining > 0:
visit(node.left) # stop once found
visit(root)
return answer
# kth smallest: iterative inorder, stop at the kth pop
def kth_smallest(root, k):
st, cur = [], root
while cur or st:
while cur:
st.append(cur)
cur = cur.left
cur = st.pop()
k -= 1
if k == 0:
return cur.val
cur = cur.right
return -1 # fewer than k nodes // kth largest: right, node, left visits values from the largest down
void reverseInorder(TreeNode* node, int& remaining, int& answer) {
if (!node) return;
reverseInorder(node->right, remaining, answer);
if (remaining > 0 && --remaining == 0) answer = node->val; // the kth visit
if (remaining > 0) reverseInorder(node->left, remaining, answer); // stop once found
}
int kthLargest(TreeNode* root, int k) {
int answer = -1;
reverseInorder(root, k, answer);
return answer;
}
// kth smallest: iterative inorder, stop at the kth pop
int kthSmallest(TreeNode* root, int k) {
stack<TreeNode*> st;
TreeNode* cur = root;
while (cur || !st.empty()) {
while (cur) { st.push(cur); cur = cur->left; }
cur = st.top(); st.pop();
if (--k == 0) return cur->val;
cur = cur->right;
}
return -1; // fewer than k nodes
} // kth largest: right, node, left visits values from the largest down
function kthLargest(root, k) {
let remaining = k, answer = -1;
(function visit(node) {
if (!node) return;
visit(node.right);
if (remaining > 0 && --remaining === 0) answer = node.val; // the kth visit
if (remaining > 0) visit(node.left); // stop once found
})(root);
return answer;
}
// kth smallest: iterative inorder, stop at the kth pop
function kthSmallest(root, k) {
const st = [];
let cur = root;
while (cur || st.length) {
while (cur) { st.push(cur); cur = cur.left; }
cur = st.pop();
if (--k === 0) return cur.val;
cur = cur.right;
}
return -1; // fewer than k nodes
} The variations
-
1Stop at the kth visit
Count every visit of an inorder walk and return at the kth. Iterative inorder makes stopping easy: just return from the loop. Reverse the walk for the kth largest.
Mistakes to avoid
- Collecting the whole inorder list just to read index k: it works, but O(n) space and no early stop.
- Passing the counter by value in C++ or as a plain argument in Python and JS: each call gets its own copy. Use a reference, a field or a closure.
- Kth largest with plain inorder needs the size of the tree first; reverse inorder does not.
Mark it done once you can write the template from memory and you have solved the worked examples yourself.