Consider a binary tree whose nodes have ids from 1 to n where n is the number of nodes in the tree. The tree is given as a collection of n pairs, where every pair represents node id and the 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 a tree data structure but it
can be solved as follows.
Every node id appears in children sum except root. So if we do the sum of all ids and subtract it from the sum of all children’s sums, we get the root.
Implementation:
C++
#include<bits/stdc++.h>
using namespace std;
int findRoot(pair< int , int > arr[], int n)
{
int root = 0;
for ( int i=0; i<n; i++)
root += (arr[i].first - arr[i].second);
return root;
}
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;
}
|
Java
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)
{
int root = 0 ;
for ( int i = 0 ; i < n; i++)
{
root += (arr[i].first - arr[i].second);
}
return root;
}
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));
}
}
|
Python3
def findRoot(arr, n) :
root = 0
for i in range (n):
root + = (arr[i][ 0 ] - arr[i][ 1 ])
return root
if __name__ = = '__main__' :
arr = [[ 1 , 5 ], [ 2 , 0 ],
[ 3 , 0 ], [ 4 , 0 ],
[ 5 , 5 ], [ 6 , 5 ]]
n = len (arr)
print (findRoot(arr, n))
|
C#
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)
{
int root = 0;
for ( int i = 0; i < n; i++)
{
root += (arr[i].first - arr[i].second);
}
return root;
}
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));
}
}
|
Javascript
<script>
const findRoot = (nodeList) => {
let root = 0;
nodeList.forEach(element =>
root+=(Number(element[0]) - Number(element[1])));
return root ;
}
let nodeList = [[1, 5], [2, 0],
[3, 0], [4, 0],
[5, 5], [6, 5]];
let root = findRoot(nodeList);
document.write(root);
</script>
|
Time Complexity: O(n), Where n is the length of the given array.
Auxiliary Space: O(1), As no extra space is used.
This article is contributed by Sunidhi Chaudhary. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.