# Find the root of the sub-tree whose weighted sum is minimum

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.

**Examples:**

Input:

Output:5

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:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `int` `ans = 0, mini = INT_MAX; ` ` ` `vector<` `int` `> graph[100]; ` `vector<` `int` `> weight(100); ` ` ` `// Function to perform dfs and update the tree ` `// such that every node's weight is the sum of ` `// the weights of all the nodes in the sub-tree ` `// of the current node including itself ` `void` `dfs(` `int` `node, ` `int` `parent) ` `{ ` ` ` `for` `(` `int` `to : graph[node]) { ` ` ` `if` `(to == parent) ` ` ` `continue` `; ` ` ` `dfs(to, node); ` ` ` ` ` `// Calculating the weighted ` ` ` `// sum of the subtree ` ` ` `weight[node] += weight[to]; ` ` ` `} ` `} ` ` ` `// Function to find the node ` `// having minimum sub-tree sum ` `void` `findMin(` `int` `n) ` `{ ` ` ` ` ` `// For every node ` ` ` `for` `(` `int` `i = 1; i <= n; i++) { ` ` ` ` ` `// If current node's weight ` ` ` `// is minimum so far ` ` ` `if` `(mini > weight[i]) { ` ` ` `mini = weight[i]; ` ` ` `ans = i; ` ` ` `} ` ` ` `} ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `n = 5; ` ` ` ` ` `// Weights of the node ` ` ` `weight[1] = -1; ` ` ` `weight[2] = 5; ` ` ` `weight[3] = -1; ` ` ` `weight[4] = 3; ` ` ` `weight[5] = -2; ` ` ` ` ` `// Edges of the tree ` ` ` `graph[1].push_back(2); ` ` ` `graph[2].push_back(3); ` ` ` `graph[2].push_back(4); ` ` ` `graph[1].push_back(5); ` ` ` ` ` `dfs(1, 1); ` ` ` `findMin(n); ` ` ` ` ` `cout << ans; ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the approach ` `ans ` `=` `0` `mini ` `=` `2` `*` `*` `32` ` ` `graph ` `=` `[[] ` `for` `i ` `in` `range` `(` `100` `)] ` `weight ` `=` `[` `0` `]` `*` `100` ` ` `# Function to perform dfs and update the tree ` `# such that every node's weight is the sum of ` `# the weights of all the nodes in the sub-tree ` `# of the current node including itself ` `def` `dfs(node, parent): ` ` ` `global` `mini, graph, weight, ans ` ` ` `for` `to ` `in` `graph[node]: ` ` ` `if` `(to ` `=` `=` `parent): ` ` ` `continue` ` ` `dfs(to, node) ` ` ` ` ` `# Calculating the weighted ` ` ` `# sum of the subtree ` ` ` `weight[node] ` `+` `=` `weight[to] ` ` ` `# Function to find the node ` `# having minimum sub-tree sum ` `def` `findMin(n): ` ` ` `global` `mini, graph, weight, ans ` ` ` ` ` `# For every node ` ` ` `for` `i ` `in` `range` `(` `1` `, n ` `+` `1` `): ` ` ` ` ` `# If current node's weight ` ` ` `# is minimum so far ` ` ` `if` `(mini > weight[i]): ` ` ` `mini ` `=` `weight[i] ` ` ` `ans ` `=` `i ` ` ` `# Driver code ` `n ` `=` `5` ` ` `# Weights of the node ` `weight[` `1` `] ` `=` `-` `1` `weight[` `2` `] ` `=` `5` `weight[` `3` `] ` `=` `-` `1` `weight[` `4` `] ` `=` `3` `weight[` `5` `] ` `=` `-` `2` ` ` `# Edges of the tree ` `graph[` `1` `].append(` `2` `) ` `graph[` `2` `].append(` `3` `) ` `graph[` `2` `].append(` `4` `) ` `graph[` `1` `].append(` `5` `) ` ` ` `dfs(` `1` `, ` `1` `) ` `findMin(n) ` ` ` `print` `(ans) ` ` ` `# This code is contributed by SHUBHAMSINGH10 ` |

*chevron_right*

*filter_none*

**Output:**

5

## Recommended Posts:

- 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
- Minimum distance to visit all the nodes of an undirected weighted tree
- 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 largest subtree having identical left and right subtrees
- Find the largest Perfect Subtree in a given Binary Tree
- Find the largest Complete Subtree in a given Binary Tree
- Minimum edge reversals to make a root
- Find Nth positive number whose digital root is X
- Find root of the tree where children id sum for every node is given

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.