Price your future right. 60% off with code BLACKSCHOLES at USD checkout. Offer ends in 1d 16h 01m 40s1 days 16 hours 1 minutes 40 seconds.
Price your future right. 60% off with code BLACKSCHOLES at USD checkout. Offer ends in 1d 16h 01m 40s1 days 16 hours 1 minutes 40 seconds.

Sign in to watch this lesson
Advanced - Trees · 4:28
A depth-first search that writes a vertex on entry and again after each child returns produces an Euler tour of 2n - 1 entries. The lowest common ancestor of u and v is the shallowest entry between their first appearances, so LCA becomes a range-minimum query over heights. A sparse table answers it with two overlapping power-of-two blocks in O(1) after O(n log n) preprocessing, which on this Mac answered 10^7 queries in about a third of a second.