Lowest Common Ancestor of a Binary Search Tree
Find the lowest node whose subtree contains both target values.
Why does this pattern fit?
Restate the exact job
Find the lowest node whose subtree contains both target values.
BST ordering shows when both targets lie on the same side.
O(h) time · O(1) space
Searching both subtrees throws away the BST advantage.
How to solve Lowest Common Ancestor of a Binary Search Tree
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
Find the lowest node whose subtree contains both target values.
Why Trees & recursive traversal fits
BST ordering shows when both targets lie on the same side.
State to maintain
One current node and the ordered target values.
Transition
Move left if both are smaller, right if both larger; otherwise current is the split.
Time and space
O(h) time · O(1) space
Counterexample to the tempting mistake
Searching both subtrees throws away the BST advantage.
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.