Data Structures & AlgorithmsVery hardICPC regional · Codeforces Div2 D

A rooted tree has these parent→child edges: 9→83, 83→70, 95→92, 87→7, 83→87, 50→90, 78→62, 83→80, 7→86, 50→78, 92→50, 78→31, 9→95, 7→43, 70→30, 92→12. A node counts as its own ancestor. What is the lowest common ancestor of the nodes 70 and 43, i.e. the deepest node that is an ancestor of both?

  1. A. 9
  2. B. 70
  3. C. 43
  4. D. 83

Correct answer: D. 83

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