Graph Theory37 sections · 1633 units
Open in Course

Core Idea - Same Depth First

(Bring nodes to same level)

If aa and bb have different depths, bring the deeper one up to the depth of the shallower one. Compute depth[v] for all nodes (via BFS or DFS).

If depth[a] > depth[b], jump aa up by depth[a] - depth[b] steps. Now aa and bb are at the same depth. Use binary lifting to perform the jump in O(log⁡n)O(\log n) time. After this step, aa and bb are at the same level and you can begin searching for their meeting point.