Consider a binary tree whose nodes have ids from 1 to n where n is number of nodes in the tree. The tree is given as a collection of n pairs, where every pair represents node id and sum of children ids.

**Examples:**

Input : 1 5 2 0 3 0 4 0 5 5 6 5 Output: 6 Explanation: In this case, two trees can be made as follows and 6 is the root node. 6 6 \ / \ 5 1 4 / \ \ 1 4 5 / \ / \ 2 3 2 3 Input : 4 0 Output: 4 Explanation: Clearly 4 does not have any children and is the only node i.e., the root node.

At first sight this question appears to be a typical question of tree data structure but it

can be solved as follows.

Every node id appears in children sum except root. So if we do sum of all ids and subtract it from sum of all children sums, we get root.

## C++

`// Find root of tree where children ` `// sum for every node id is given. ` `#include<bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `int` `findRoot(pair<` `int` `, ` `int` `> arr[], ` `int` `n) ` `{ ` ` ` `// Every node appears once as an id, and ` ` ` `// every node except for the root appears ` ` ` `// once in a sum. So if we subtract all ` ` ` `// the sums from all the ids, we're left ` ` ` `// with the root id. ` ` ` `int` `root = 0; ` ` ` `for` `(` `int` `i=0; i<n; i++) ` ` ` `root += (arr[i].first - arr[i].second); ` ` ` ` ` `return` `root; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `pair<` `int` `, ` `int` `> arr[] = {{1, 5}, {2, 0}, ` ` ` `{3, 0}, {4, 0}, {5, 5}, {6, 5}}; ` ` ` `int` `n = ` `sizeof` `(arr)/` `sizeof` `(arr[0]); ` ` ` `printf` `(` `"%d\n"` `, findRoot(arr, n)); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Find root of tree where children ` `// sum for every node id is given. ` ` ` `class` `GFG ` `{ ` ` ` ` ` `static` `class` `pair ` ` ` `{ ` ` ` ` ` `int` `first, second; ` ` ` ` ` `public` `pair(` `int` `first, ` `int` `second) ` ` ` `{ ` ` ` `this` `.first = first; ` ` ` `this` `.second = second; ` ` ` `} ` ` ` ` ` `} ` ` ` ` ` `static` `int` `findRoot(pair arr[], ` `int` `n) ` ` ` `{ ` ` ` `// Every node appears once as an id, and ` ` ` `// every node except for the root appears ` ` ` `// once in a sum. So if we subtract all ` ` ` `// the sums from all the ids, we're left ` ` ` `// with the root id. ` ` ` `int` `root = ` `0` `; ` ` ` `for` `(` `int` `i = ` `0` `; i < n; i++) ` ` ` `{ ` ` ` `root += (arr[i].first - arr[i].second); ` ` ` `} ` ` ` ` ` `return` `root; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `pair arr[] = {` `new` `pair(` `1` `, ` `5` `), ` `new` `pair(` `2` `, ` `0` `), ` ` ` `new` `pair(` `3` `, ` `0` `), ` `new` `pair(` `4` `, ` `0` `), ` ` ` `new` `pair(` `5` `, ` `5` `), ` `new` `pair(` `6` `, ` `5` `)}; ` ` ` `int` `n = arr.length; ` ` ` `System.out.printf(` `"%d\n"` `, findRoot(arr, n)); ` ` ` `} ` ` ` `} ` ` ` `/* This code is contributed by PrinciRaj1992 */` |

*chevron_right*

*filter_none*

## Python3

`"""Find root of tree where children ` `sum for every node id is given"""` ` ` `def` `findRoot(arr, n) : ` ` ` ` ` `# Every node appears once as an id, and ` ` ` `# every node except for the root appears ` ` ` `# once in a sum. So if we subtract all ` ` ` `# the sums from all the ids, we're left ` ` ` `# with the root id. ` ` ` `root ` `=` `0` ` ` `for` `i ` `in` `range` `(n): ` ` ` `root ` `+` `=` `(arr[i][` `0` `] ` `-` `arr[i][` `1` `]) ` ` ` `return` `root ` ` ` `# Driver Code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` ` ` `arr ` `=` `[[` `1` `, ` `5` `], [` `2` `, ` `0` `], ` ` ` `[` `3` `, ` `0` `], [` `4` `, ` `0` `], ` ` ` `[` `5` `, ` `5` `], [` `6` `, ` `5` `]] ` ` ` `n ` `=` `len` `(arr) ` ` ` `print` `(findRoot(arr, n)) ` ` ` `# This code is contributed ` `# by SHUBHAMSINGH10 ` |

*chevron_right*

*filter_none*

## C#

`// C# Find root of tree where children ` `// sum for every node id is given. ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` ` ` `public` `class` `pair ` ` ` `{ ` ` ` ` ` `public` `int` `first, second; ` ` ` ` ` `public` `pair(` `int` `first, ` `int` `second) ` ` ` `{ ` ` ` `this` `.first = first; ` ` ` `this` `.second = second; ` ` ` `} ` ` ` ` ` `} ` ` ` ` ` `static` `int` `findRoot(pair []arr, ` `int` `n) ` ` ` `{ ` ` ` `// Every node appears once as an id, and ` ` ` `// every node except for the root appears ` ` ` `// once in a sum. So if we subtract all ` ` ` `// the sums from all the ids, we're left ` ` ` `// with the root id. ` ` ` `int` `root = 0; ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `{ ` ` ` `root += (arr[i].first - arr[i].second); ` ` ` `} ` ` ` ` ` `return` `root; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main(String[] args) ` ` ` `{ ` ` ` `pair []arr = {` `new` `pair(1, 5), ` `new` `pair(2, 0), ` ` ` `new` `pair(3, 0), ` `new` `pair(4, 0), ` ` ` `new` `pair(5, 5), ` `new` `pair(6, 5)}; ` ` ` `int` `n = arr.Length; ` ` ` `Console.Write(` `"{0}\n"` `, findRoot(arr, n)); ` ` ` `} ` ` ` `} ` ` ` `/* This code is contributed by PrinciRaj1992 */` |

*chevron_right*

*filter_none*

**Output:**

6

This article is contributed by **Sunidhi Chaudhary**. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

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:

- Node having maximum sum of immediate children and itself in n-ary tree
- Number of children of given node in n-ary Tree
- Queries to find sum of distance of a given node to every leaf node in a Weighted Tree
- Convert an arbitrary Binary Tree to a tree that holds Children Sum Property
- General Tree (Each node can have arbitrary number of children) Level Order Traversal
- Find if there is a pair in root to a leaf path with sum equals to root's data
- Check for Children Sum Property in a Binary Tree
- Maximum parent children sum in Binary tree
- Iterative approach to check for children sum property in a Binary Tree
- Find distance from root to given node in a binary tree
- Maximum XOR with given value in the path from root to given node in the tree
- Given a n-ary tree, count number of nodes which have more number of children than parents
- Number of full binary trees such that each node is product of its children
- Print path from root to a given node in a binary tree
- Sort the path from root to a given node in a Binary Tree
- Count of nodes in a Binary tree with immediate children as its factors
- Count nodes with two children at level L in a Binary Tree
- Count of nodes in a Binary Tree whose immediate children are co-prime
- Find the maximum sum leaf to root path in a Binary Tree
- Find the root of the sub-tree whose weighted sum XOR with X is maximum