Find the Root of a Tree from the Sum of Child Node IDs
Last Updated : 12 Dec, 2024
Given a Binary Tree with nodes labelled from 1 to n, where n is the total number of nodes. The task is to identify the root of binary tree given that each node is described by a pair consisting of its id and the sum of its children's id's.
Examples:
Input: [[1, 5], [2, 0], [3, 0], [4, 0], [5, 5], [6, 5]] Output: 6 Explanation: In this question, two trees having 6 as their root node can me generated.
Input: [[4, 0] Output: 4 Explanation: 4 is the only node in a tree and thus does not have any child as well.
Approach:
The idea is to use the property that every node id, except for the root, appears in the sum of children ids. By calculating the total sum of all node id's and subtracting it to the total sum of all children sum, the difference will be the id of the root node.
C++
// C++ Program to Find root of tree where children// sum for every node id is given.#include<bits/stdc++.h>usingnamespacestd;intfindRoot(vector<pair<int,int>>&arr){// Subtract the sum of children// from the sum of all nodesintroot=0;for(inti=0;i<arr.size();i++){// Add the node Id to the rootroot+=arr[i].first;// Subtract the sum of childrenroot-=arr[i].second;}returnroot;}intmain(){vector<pair<int,int>>arr={{1,5},{2,0},{3,0},{4,0},{5,5},{6,5}};// The given array represents the following binary tree// 6 6// \ / \ // 5 1 4// / \ \ // 1 4 5// / \ / \ // 2 3 2 3intres=findRoot(arr);cout<<res<<endl;return0;}
Java
// Java Program to Find root of tree where// children sum for every node id is given.classGfG{staticclasspair{intfirst,second;publicpair(intfirst,intsecond){this.first=first;this.second=second;}}staticintfindRoot(pairarr[]){// Subtract the sum of children// from the sum of all nodesintroot=0;for(inti=0;i<arr.length;i++){// Add the node Id to the rootroot+=arr[i].first;// Subtract the sum of childrenroot-=arr[i].second;}// Return the root of the treereturnroot;}publicstaticvoidmain(String[]args){pairarr[]={newpair(1,5),newpair(2,0),newpair(3,0),newpair(4,0),newpair(5,5),newpair(6,5)};// The given array represents the following binary// tree// 6 6// \ / \// 5 1 4// / \ \// 1 4 5// / \ / \// 2 3 2 3System.out.print(findRoot(arr));}}
Python
# Python Program to Find root of tree where # children sum for every node id is given.deffindRoot(arr):# Subtract the sum of children# from the sum of all nodesroot=0foriinrange(len(arr)):# Add the node Id to the rootroot+=arr[i][0]# Subtract the sum of childrenroot-=arr[i][1]# Return the root of the treereturnrootif__name__=='__main__':arr=[[1,5],[2,0],[3,0],[4,0],[5,5],[6,5]]#The given array represents the following binary tree# 6 6# \ / \# 5 1 4# / \ \# 1 4 5# / \ / \# 2 3 2 3print(findRoot(arr))
C#
// C# Program to Find root of tree where// children sum for every node id is given.usingSystem;classGfG{publicclasspair{publicintfirst,second;publicpair(intfirst,intsecond){this.first=first;this.second=second;}}staticintfindRoot(pair[]arr){// Subtract the sum of children// from the sum of all nodesintroot=0;for(inti=0;i<arr.Length;i++){// Add the node Id to the rootroot+=arr[i].first;// Subtract the sum of childrenroot-=arr[i].second;}// Return the root of the treereturnroot;}staticvoidMain(String[]args){pair[]arr={newpair(1,5),newpair(2,0),newpair(3,0),newpair(4,0),newpair(5,5),newpair(6,5)};// The given array represents the following binary// tree// 6 6// \ / \// 5 1 4// / \ \// 1 4 5// / \ / \// 2 3 2 3Console.Write(findRoot(arr));}}
JavaScript
// JavaScript to Find root of tree where// children sum for every node id is given.functionfindRoot(arr){// Subtract the sum of children// from the sum of all nodesletroot=0;for(leti=0;i<arr.length;i++){// Add the node Id to the rootroot+=arr[i][0];// Subtract the sum of childrenroot-=arr[i][1];}// Return the root of the treereturnroot;}letarr=[[1,5],[2,0],[3,0],[4,0],[5,5],[6,5]];// The given array represents the following binary tree// 6 6// \ / \// 5 1 4// / \ \// 1 4 5// / \ / \// 2 3 2 3console.log(findRoot(arr));
Output
6
Time Complexity: O(n), Where n is the length of the given array. Auxiliary Space: O(1), As no extra space is used.