Why it matters

LCA underlies many tree algorithms. Understanding enables tree problem solving.

Advertisement

The architecture

Binary lifting: for each node, store 2^k-th ancestor.

Euler tour + RMQ: linearize tree; LCA = RMQ on depth.

LCA approachesBinary lifting2^k ancestorsEuler tour + RMQlinearizeQueryO(log n) or O(1)Binary lifting: simpler code, O(log n) query. Euler + sparse table RMQ: O(1) query
LCA methods.
Advertisement

How it works end to end

Binary lifting: precompute in O(n log n); query O(log n).

Euler tour + sparse table RMQ: O(n log n) precomp; O(1) query.

Applications: path length = depth(u) + depth(v) - 2*depth(LCA).