Kth Smallest Element in a BST
Return the kth value in BST sorted order.
Why does this pattern fit?
Restate the exact job
Return the kth value in BST sorted order.
In-order traversal visits BST values in ascending order.
O(h + k) time · O(h) space
Stopping after k pushes confuses discovery with visitation.
How to solve Kth Smallest Element in a BST
The goal is to solve this problem from the pattern, not to memorize a finished answer. Use this as a check after your own attempt.
What the question asks
Return the kth value in BST sorted order.
Why Trees & recursive traversal fits
In-order traversal visits BST values in ascending order.
State to maintain
An explicit stack, current node, and remaining rank.
Transition
Push left paths, pop one value at a time, decrement k, then explore right.
Time and space
O(h + k) time · O(h) space
Counterexample to the tempting mistake
Stopping after k pushes confuses discovery with visitation.
Prove it again tomorrow
Close this page. Rebuild the state and transition from memory, write a test that exposes the mistake above, then solve a fresh input without looking back. A same-day reread is practice, not proof of retention.