Data Structures & AlgorithmsModerateGATE CS · TCS NQT

A rooted tree on vertices 0 to 11 has parent[i] = [0, 0, 2, 1, 0, 1, 4, 4, 4, 6, 3] for i = 1 to 11 (vertex 0 is the root). What is the sum, over all vertices v, of the number of vertices in the subtree rooted at v (v itself included)?

  1. A. 24
  2. B. 36
  3. C. 78
  4. D. 12

Correct answer: B. 36

The subtree size of v counts v itself, and summing sizes equals summing (depth + 1) over all vertices. Summing depths forgets the +1 for each vertex.

More Trees, Graphs, Recursion and Dynamic Programming questions

Exams that ask this topic