Data Structures & AlgorithmsModerateICPC regional · Codeforces Div2 D
A rooted tree has these parent→child edges: 39→45, 99→70, 25→48, 13→64, 99→25, 25→13, 73→94, 25→73, 25→39. A node counts as its own ancestor. What is the lowest common ancestor of the nodes 64 and 48, i.e. the deepest node that is an ancestor of both?
- A. 25
- B. 99
- C. 13
- D. 48
Correct answer: A. 25
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.
Practise more Trees, Graphs, Recursion and Dynamic Programming questionsTrees, Graphs, Recursion and Dynamic Programming: notes and key points
More Trees, Graphs, Recursion and Dynamic Programming questions
- A rooted tree has these parent→child edges: 98→34, 27→69, 27→30, 69→98, 82→14, 69→43, 30→67, 30→82. A node counts as its own ancestor…
- A rooted tree has these parent→child edges: 53→34, 22→91, 91→2, 59→99, 91→24, 24→59, 99→53. A node counts as its own ancestor. What is the…
- 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…
- A rooted tree has these parent→child edges: 98→57, 49→29, 72→94, 76→98, 72→87, 76→41, 76→49, 94→71, 72→76, 57→11. A node counts as its own…
- A rooted tree has these parent→child edges: 96→13, 13→90, 13→32, 72→46, 58→36, 13→30, 14→96, 72→93, 90→58, 96→50, 20→72, 58→20, 50→61. A…
- 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…