Phase 0: Cold-start — problem statement, input and output, first prediction.

This explainer has 9 phases — cold-start, generic-LCA wall, BST handshake, descent, code bridge, parameter explorer, edge cases, synthesis, and complete.

LC 235 — Problem

Given a Binary Search Tree and two of its node values p and q, return the lowest common ancestor — the deepest node that has both p and q somewhere in its subtree.

FIG. 1 — INPUT — BST WITH TARGETS P=1 AND Q=3

— A balanced 15-node BST. Two values are marked: p = 1, q = 3.

FIG. 2 — OUTPUT — ONE NODE, THE DEEPEST ONE THAT CONTAINS BOTH TARGETS

— Output shape: a single node from this tree — the deepest one whose subtree still contains both p and q. Your answer goes below ↓

The generic LCA algorithm — the one that works on any binary tree — has to inspect every node to confirm both targets live below it. But this is a Binary Search Tree. It has a property the generic algorithm ignores entirely. How much does that cost?

Look at the tree. Of the four candidates below, which one is the deepest node that has BOTH 1 and 3 somewhere underneath it?

You eyeballed the answer. Easy on a 15-node BST. The hard question is: how do we GET there in code, fast, without staring at the picture? That is where the next phase walks straight into the wall.