Given a tree, and the weights of all the nodes, the task is to find the root of the sub-tree whose weighted sum is minimum.
Weight of sub-tree for parent 1 = ((-1) + (5) + (-2) + (-1) + (3)) = 4
Weight of sub-tree for parent 2 = ((5) + (-1) + (3)) = 7
Weight of sub-tree for parent 3 = -1
Weight of sub-tree for parent 4 = 3
Weight of sub-tree for parent 5 = -2
Node 5 gives the minimum sub-tree weighted sum.
Approach: Perform dfs on the tree, and for every node calculate the sub-tree weighted sum rooted at the current node then find the minimum sum value for a node.
Below is the implementation of the above approach:
- Find the root of the sub-tree whose weighted sum XOR with X is minimum
- Find the root of the sub-tree whose weighted sum XOR with X is maximum
- Check if two nodes are in same subtree of the root node
- Find if there is a pair in root to a leaf path with sum equals to root's data
- Minimum cost to connect weighted nodes represented as array
- Find largest subtree sum in a tree
- Find the Kth node in the DFS traversal of a given subtree in a Tree
- Find the largest BST subtree in a given Binary Tree | Set 1
- Find the largest Perfect Subtree in a given Binary Tree
- Find the largest Complete Subtree in a given Binary Tree
- Find largest subtree having identical left and right subtrees
- Minimum edge reversals to make a root
- Find root of the tree where children id sum for every node is given
- Find Nth positive number whose digital root is X
- Find distance from root to given node in a binary tree
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.