Subtree Node Count Problem
We are provided with a tree containing 'N' nodes, numbered from 0 to N-1. The objective is to determine the total number of nodes within each subtree of the provided tree. Specifically, for each node in a general tree, calculate the count of elements in the subtrees rooted at each node.
Input:
T # Number of test cases
For each test case:
N # Number of nodes in the tree
N-1 lines containing two integers U and V indicating an edge between node U and node V
Output:
For each test case, output the number of elements in all subtrees of the tree, in any order.
Example:
Consider an image representation of the tree for clarity.
Constraints:
1 ≤ T ≤ 100
1 ≤ N, E ≤ 104
0 ≤ U,V < N
- Time Limit: 1 second
Note:
1. The tree will always have its root at node 0.
2. Answers can be presented in any sequence.
3. A node that is a leaf will have a subtree size of 1 (only the node itself).
Be the first one to answer
Add answer anonymously...
Top ThoughtWorks Software Developer interview questions & answers
Popular interview questions of Software Developer
Top HR questions asked in ThoughtWorks Software Developer
>
ThoughtWorks Software Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+
Reviews
4 L+
Interviews
4 Cr+
Salaries
1 Cr+
Users/Month
Contribute to help millions
Get AmbitionBox app