Data Structures & AlgorithmsVery hardCodeforces Div2 B · TCS CodeVita

A rooted tree has these parent→child edges: 23→29, 11→4, 9→55, 86→3, 21→36, 9→16, 16→39, 39→21, 9→86, 21→15, 36→33, 15→23, 9→8, 29→11. The root is at depth 0. How many nodes lie at depth exactly 2?

  1. A. 4
  2. B. 1
  3. C. 7
  4. D. 2

Correct answer: D. 2

Count the nodes whose depth is exactly 2, by computing each node's depth from the root. Off-by-one on the depth, or counting cumulatively, gives the trap values.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic