← Tree Recursion

Pattern Recognition Drill

#89 — Lowest common ancestor

Medium Advanced Trees

The Problem

Find the LCA of two nodes in a binary tree.

What approach would you use?

Think about it before scrolling down.

Key Signals

Tree Recursion

Recurse left and right. If both return non-null, current node is LCA. If one is null, return the other. O(n).

Common Trap

For BSTs, you can use the BST property to go left/right more efficiently. But for general trees, full recursion is needed.

← #88 #90 →