Given a tree, and the weights of all the nodes, the task is to find the root of the sub-tree whose weighted sum XOR with given integer **X** is minimum.

**Examples:**

Input:

X = 15

Output:5

Weight of sub-tree for parent 1 = ((-1) + (5) + (-2) + (-1) + (3)) XOR 15 = 4 XOR 15 = 11

Weight of sub-tree for parent 2 = ((5) + (-1) + (3)) XOR 15 = 7 XOR 15 = 8

Weight of sub-tree for parent 3 = -1 XOR 15 = -16

Weight of sub-tree for parent 4 = 3 XOR 15 = 12

Weight of sub-tree for parent 5 = -2 XOR 15 = -15

Node 3 gives the minimum sub-tree weighted sum XOR X.

**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 XOR X) 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 XOR x ` `void` `findMinX(` `int` `n, ` `int` `x) ` `{ ` ` ` ` ` `// For every node ` ` ` `for` `(` `int` `i = 1; i <= n; i++) { ` ` ` ` ` `// If current node's weight XOR x ` ` ` `// is minimum so far ` ` ` `if` `(mini > (weight[i] ^ x)) { ` ` ` `mini = (weight[i] ^ x); ` ` ` `ans = i; ` ` ` `} ` ` ` `} ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `x = 15; ` ` ` `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); ` ` ` `findMinX(n, x); ` ` ` ` ` `cout << ans; ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `import` `java.util.*; ` ` ` `class` `GFG ` `{ ` ` ` `static` `int` `ans = ` `0` `, mini = Integer.MAX_VALUE; ` ` ` ` ` `static` `Vector<Integer>[] graph = ` `new` `Vector[` `100` `]; ` ` ` `static` `Integer[] weight = ` `new` `Integer[` `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 ` ` ` `static` `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 XOR x ` ` ` `static` `void` `findMinX(` `int` `n, ` `int` `x) ` ` ` `{ ` ` ` ` ` `// For every node ` ` ` `for` `(` `int` `i = ` `1` `; i <= n; i++) ` ` ` `{ ` ` ` ` ` `// If current node's weight XOR x ` ` ` `// is minimum so far ` ` ` `if` `(mini > (weight[i] ^ x)) ` ` ` `{ ` ` ` `mini = (weight[i] ^ x); ` ` ` `ans = i; ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `int` `x = ` `15` `; ` ` ` `int` `n = ` `5` `; ` ` ` `for` `(` `int` `i = ` `0` `; i < ` `100` `; i++) ` ` ` `graph[i] = ` `new` `Vector<Integer>(); ` ` ` ` ` `// 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` `].add(` `2` `); ` ` ` `graph[` `2` `].add(` `3` `); ` ` ` `graph[` `2` `].add(` `4` `); ` ` ` `graph[` `1` `].add(` `5` `); ` ` ` ` ` `dfs(` `1` `, ` `1` `); ` ` ` `findMinX(n, x); ` ` ` ` ` `System.out.print(ans); ` ` ` `} ` `} ` ` ` `// This code is contributed by Rajput-Ji ` |

*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` `ans, mini, graph, weight, x ` ` ` `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 XOR x ` `def` `findMinX(n, x): ` ` ` `global` `ans, mini,graph,weight ` ` ` ` ` `# For every node ` ` ` `for` `i ` `in` `range` `(` `1` `, n ` `+` `1` `): ` ` ` ` ` `# If current node's weight XOR x ` ` ` `# is minimum so far ` ` ` `if` `(mini > (weight[i] ^ x)): ` ` ` `mini ` `=` `(weight[i] ^ x) ` ` ` `ans ` `=` `i ` ` ` `# Driver code ` `x ` `=` `15` `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` `) ` `findMinX(n, x) ` ` ` `print` `(ans) ` ` ` `# This code is contributed by SHUBHAMSINGH10 ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` `using` `System; ` `using` `System.Collections.Generic; ` ` ` `class` `GFG ` `{ ` ` ` `static` `int` `ans = 0, mini = ` `int` `.MaxValue; ` ` ` ` ` `static` `List<` `int` `>[] graph = ` `new` `List<` `int` `>[100]; ` ` ` `static` `int` `[] weight = ` `new` `int` `[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 ` ` ` `static` `void` `dfs(` `int` `node, ` `int` `parent) ` ` ` `{ ` ` ` `foreach` `(` `int` `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 XOR x ` ` ` `static` `void` `findMinX(` `int` `n, ` `int` `x) ` ` ` `{ ` ` ` ` ` `// For every node ` ` ` `for` `(` `int` `i = 1; i <= n; i++) ` ` ` `{ ` ` ` ` ` `// If current node's weight XOR x ` ` ` `// is minimum so far ` ` ` `if` `(mini > (weight[i] ^ x)) ` ` ` `{ ` ` ` `mini = (weight[i] ^ x); ` ` ` `ans = i; ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main(String[] args) ` ` ` `{ ` ` ` `int` `x = 15; ` ` ` `int` `n = 5; ` ` ` `for` `(` `int` `i = 0; i < 100; i++) ` ` ` `graph[i] = ` `new` `List<` `int` `>(); ` ` ` ` ` `// 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].Add(2); ` ` ` `graph[2].Add(3); ` ` ` `graph[2].Add(4); ` ` ` `graph[1].Add(5); ` ` ` ` ` `dfs(1, 1); ` ` ` `findMinX(n, x); ` ` ` ` ` `Console.Write(ans); ` ` ` `} ` `} ` ` ` `// This code is contributed by PrinciRaj1992 ` |

*chevron_right*

*filter_none*

**Output:**

3

__Complexity Analysis:__

**Time Complexity :**O(N).

In dfs, every node of the tree is processed once and hence the complexity due to the dfs is O(N) if there are total N nodes in the tree. Therefore, the time complexity is O(N).**Auxiliary Space :**O(n).

Recursion stack space can be up to O(n).

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Find the root of the sub-tree whose weighted sum XOR with X is maximum
- Find the root of the sub-tree whose weighted sum is minimum
- 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
- Count the nodes of the given tree whose weighted string is a palindrome
- Count the nodes of the tree whose weighted string contains a vowel
- Count the nodes of a tree whose weighted string does not contain any duplicate characters
- Count of leaf nodes of the tree whose weighted string is a palindrome
- Count the nodes of a tree whose weighted string is an anagram of the given string
- Minimum difference between any two weighted nodes in Sum Tree of the given Tree
- Queries to find the Minimum Weight from a Subtree of atmost D-distant Nodes from Node X
- Find the node whose xor with x gives minimum value
- Find largest subtree sum in a tree
- Queries to find sum of distance of a given node to every leaf node in a Weighted Tree
- Minimum number of Nodes to be removed such that no subtree has more than K nodes
- Minimum cost to connect weighted nodes represented as array
- Minimum distance to visit all the nodes of an undirected weighted tree
- Minimum Cost of Simple Path between two nodes in a Directed and Weighted Graph
- Find Nth positive number whose digital root is X
- Subtree with given sum 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 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.