If the given tree:
And POINTS = [3,5,1,4] and X = 2
The answer will be 5 (Points of 2 + Points of 3).
The first line of the input contains an integer, 'T,’ denoting the number of test cases.
The first line of each test case contains a single integer ‘N’ denoting the number of kingdoms.
The next ‘N’-1 lines of each test case contain two integers representing an edge between the given indices.
The next line contains the ‘POINTS’ array.
The next line contains a single integer ‘X’.
For each test case, print the sum of importance points of Employee ‘X’ and all its subordinates.
Print the output of each test case in a separate line.
You do not need to print anything. It has already been taken care of. Just implement the given function.
1 <= T <= 10
1 <= N <= 10^6.
1 <= POINTS[i] <= 1000
0 <= X < N.
Time limit: 1 sec
In this approach, we will travel to all the nodes in a subtree of ‘X’ and return the sum of points of all subordinates.We will define a recursive function DFS(‘CUR’,’TREE’,’POINTS’) which will return the answer for the Node having ID as ‘X’ and we will make the recursive call for all of his direct subordinates.
At last,we will return the answer for DFS(X, TREE, POINTS).
In this approach, we will traverse the subtree of ‘X’ using breadth first search .We will use a queue to store the traverse and will maintain a variable ‘ANS’ that will store the sum of points.