PROBLEM 26 OF 75

Lowest Common Ancestor of a Binary Search Tree

Find the lowest node whose subtree contains both target values.

PATTERNTrees & recursive traversalDefine the meaning of one recursive return value.
SPOT THE SIGNAL

Why does this pattern fit?

FRAME · 1 OF 4

Restate the exact job

Find the lowest node whose subtree contains both target values.

WHY THIS FITS

BST ordering shows when both targets lie on the same side.

COMPLEXITY

O(h) time · O(1) space

COMMON FAILURE

Searching both subtrees throws away the BST advantage.

THE REASONING, IN ONE PLACE

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.

Open blank recall ↗