Data Structures & AlgorithmsHardGATE CS · TCS NQT
A rooted tree on vertices 0 to 10 has parent[i] = [0, 0, 0, 2, 2, 1, 1, 1, 1, 5] for i = 1 to 10 (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)?
- A. 11
- B. 66
- C. 29
- D. 18
Correct answer: C. 29
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.
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 on vertices 0 to 10 has parent[i] = [0, 1, 1, 1, 1, 2, 1, 1, 8, 5] for i = 1 to 10 (vertex 0 is the root). What is the sum…
- A rooted tree on vertices 0 to 11 has parent[i] = [0, 1, 0, 2, 3, 0, 5, 7, 0, 8, 1] for i = 1 to 11 (vertex 0 is the root). What is the…
- A rooted tree on vertices 0 to 7 has parent[i] = [0, 1, 0, 3, 2, 4, 1] for i = 1 to 7 (vertex 0 is the root). What is the sum, over all…
- The keys [55, 6, 10, 36, 89, 11, 26, 80, 54, 86, 33] are inserted, in this order, into an initially empty binary search tree (no…
- The keys [47, 99, 2, 94, 90, 88, 97, 6, 8, 60, 30] are inserted, in this order, into an initially empty binary search tree (no…
- The keys [23, 4, 71, 60, 98, 7, 85] are inserted, in this order, into an initially empty binary search tree (no rebalancing). How many…