workshop private

← all creations

Lowest Common

viz · created 2026-10-04

The LC 236 lowest-common-ancestor walk answered bottom-up rather than searched top-down — every node lights with only what came back from its children (nothing, left, right, both), the first to light with both is the answer, and dragging one marker onto an ancestor of the other shows the same rule absorbing the trap case with no special code.

algorithmsinterview-prepcanvas

Lowest Common Ancestor of a Binary Tree (LC 236): two marked nodes p and q, find the lowest node that has both below it — where a node counts as its own descendant. The piece runs the post-order pass one event at a time: enter a node, come back out of it, and when you come back out, hand something up. The thing to watch is that no node ever learns where it sits in the tree. It learns one fact from each child — did a target turn up on that side — and the whole algorithm is what it does with two facts.

Each node, once returned from, wears what its children told it: the left half of its ring lights blue when the left side found something, the right half when the right side did. One side lit means the node passes that find up, untouched — the blue arrow on the edge above it is the find travelling. Both halves lit is the answer, ringed amber: the paths from p and q join here, and the node knows it without ever having seen the root. The tag under every node is what it handed up — ∅ for nothing, ↑5 for a node.

Drag p onto an ancestor of q (the ancestor preset does it for you: p = 5, q = 4 on the brief’s tree) and the rule that looks like it should need a special case does not get one. The walk reaches 5, 5 is a target, 5 hands itself up without looking below — four nodes are drawn dashed and never entered — and everything else in the tree reports nothing, so 5 is what arrives at the root. The walk never found q. It did not have to: both targets are guaranteed to exist, and that guarantee is doing the work.

Three rules, switchable, because the second and third are what people write when they do not trust the first:

Type any LeetCode level-order array into the tree box, or roll a random one.

Reuse

src/lowest-common.js is a framework-free ES module:

No rendering or timers in the module; the canvas demo is reference code.

Gotchas