Data Structures & AlgorithmsHardICPC regional · Codeforces Div2 D

A rooted tree has these parent→child edges: 63→16, 38→40, 21→76, 76→77, 38→63, 55→21, 38→55, 21→60, 76→99. A node counts as its own ancestor. What is the lowest common ancestor of the nodes 16 and 55, i.e. the deepest node that is an ancestor of both?

  1. A. 63
  2. B. 55
  3. C. 38
  4. D. 16

Correct answer: C. 38

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