Data Structures & AlgorithmsHardICPC regional · Codeforces Div2 D

A rooted tree has these parent→child edges: 47→3, 80→83, 80→90, 80→77, 56→43, 80→30, 3→80, 80→50, 3→34, 30→18, 3→56, 47→68. A node counts as its own ancestor. What is the lowest common ancestor of the nodes 34 and 68, i.e. the deepest node that is an ancestor of both?

  1. A. 3
  2. B. 68
  3. C. 34
  4. D. 47

Correct answer: D. 47

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