
If N = 5 and Assets = { 10, 20, 30, 40, 50 } and tree is denoted as:

Then we have 4 choices of deletion:
Edge 0-1 is deleted: now, the one of the tree contains node-1 and node-3, and the other contains node-0, node-2 and node-3, subtree difference is = 90 - 60 = 30
Edge 1-3 is deleted: subtree difference is = 110 - 40 = 70
Edge 0-2 is deleted: subtree difference is = 80 - 70 = 10
Edge 2-4 is deleted: subtree difference is = 100 - 50 = 50
Clearly removing the edge between node-0 and node-2 leads to the most optimal partition where the difference is equal to 10. Hence we return the value 10.
The first line contains a single integer ‘T’ denoting the number of test cases, then each test case follows:
The first line of each test case contains a single integer ‘N’ denoting the number of assets.
The next line contains N integers, where Assets[i] denotes the asset-value of the ith asset.
The next N-1 lines each contain two integers ‘U’ and ‘V’ denoting that there is an edge between node-U and node-V.
For each test case, print the lowest possible difference between assets after splitting.
Output for each test case will be printed in a separate line.
You are not required to print anything; it has already been taken care of. Just implement the function.
1 ≤ T ≤ 10
2 ≤ N ≤ 200
0 ≤ Assets[i] ≤ 10^6
0 ≤ U, V ≤ N-1
Time limit: 1 sec
Try to remove the edges one by one to find the optimal partition.
The steps are as follows :
Since the total asset value is equal to the sum of the asset values of each node, the ideal partition would be one in which one of the children is given assets as close as half the total asset value. Partition involves splitting the current tree into two trees, hence we simply need to find a subtree that has its sum closest to half of the total assets, we can then simply partition the tree about the edge that connects it with the remaining tree.
To find the optimal subtree we need not have to calculate the sum of each subtree one by one, rather we can use a single depth-first search call and use the precomputed sum to directly find the sum of the subtree more efficiently, that is: the sum of the subtree rooted at ith node is equal to the sum of the asset value of ith node and the sum of all the subtrees of children of I.
The steps are as follows :
Inorder Traversal
Inorder Traversal
Inorder Traversal
Inorder Traversal
Inorder Traversal
Postorder Traversal
Postorder Traversal
Height of Binary Tree
Height of Binary Tree
Height of Binary Tree
Height of Binary Tree
Locked Binary Tree
Maximum Island Size in a Binary Tree