Data Structures & AlgorithmsHardCodeforces Div2 B · TCS CodeVita

A rooted tree has these parent→child edges: 24→82, 24→60, 22→26, 82→59, 65→24, 71→23, 26→71, 65→22, 26→47, 75→65. The root is at depth 0. How many nodes lie at depth exactly 2?

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

Correct answer: B. 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