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)?
- A. 24
- B. 36
- C. 78
- 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.
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 7 has parent[i] = [0, 0, 2, 3, 0, 0, 5] for i = 1 to 7 (vertex 0 is the root). What is the sum, over all…
- A rooted tree on vertices 0 to 5 has parent[i] = [0, 0, 2, 3, 0] for i = 1 to 5 (vertex 0 is the root). What is the sum, over all vertices…
- What is the length of the longest strictly increasing subsequence (not necessarily contiguous) of [3, 7, 4, 1, 8, 5, 6, 2, 9]?
- What is the length of the longest strictly increasing subsequence (not necessarily contiguous) of [13, 9, 11, 2, 6, 4, 7, 10, 1, 5, 8, 12…
- What is the length of the longest strictly increasing subsequence (not necessarily contiguous) of [9, 3, 1, 8, 13, 6, 2, 5, 10, 14, 11, 7…
- What is the length of the longest strictly increasing subsequence (not necessarily contiguous) of [2, 1, 6, 5, 10, 4, 7, 9, 3, 11, 8]?