Data Structures & AlgorithmsModerateCodeforces Div2 B · TCS CodeVita

A rooted tree has these parent→child edges: 24→10, 89→82, 17→89, 17→93, 89→66, 82→24, 93→25. The root is at depth 0. How many nodes lie at depth exactly 2?

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

Correct answer: A. 3

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