Data Structures & AlgorithmsVery hardICPC regional · Codeforces Div2 D

A rooted tree has these parent→child edges: 4→14, 35→69, 35→83, 70→56, 4→37, 69→26, 4→68, 35→70, 56→99, 70→24, 44→4, 78→44, 2→35, 78→2. A node counts as its own ancestor. What is the lowest common ancestor of the nodes 37 and 24, i.e. the deepest node that is an ancestor of both?

  1. A. 4
  2. B. 78
  3. C. 37
  4. D. 24

Correct answer: B. 78

The LCA is the deepest common ancestor: lift the deeper node to the same depth, then climb both together (binary lifting does this in O(log n)). Answering the root, the parent of one node, or a node itself is the trap.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic