The Wayback Machine - https://web.archive.org/web/20240910144832/https://www.geeksforgeeks.org/find-root-tree-children-id-sum-every-node-given/
Open In App

Find root of the tree where children id sum for every node is given

Last Updated : 29 Jul, 2022
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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++





Java





Python3





C#





Javascript





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.

 



Similar Reads

Minimize changes to convert into Tree with root 1, even left children and odd right children
Given, a binary tree, the task is to convert this tree using minimum number of increment-decrement operations into a tree which satisfies the following conditions: The root node is always 1.Every left child of a node is even.Every right child of a node is odd. Return and print the minimum number of increment-decrement operations required at the end
10 min read
Minimize sum of node values by filling given empty Tree such that each node is GCD of its children
Given a Binary Tree consisting of N nodes having no values in it and an integer X, that represents the value of the root node, the task is to find the minimum sum of all the nodes value of the given Tree such that the value of each node must be the value of GCDs of its children. Also, no two siblings can have the same value. Examples: Input: Output
11 min read
Queries to find sum of distance of a given node to every leaf node in a Weighted Tree
Given a Undirected Weighted Tree having N nodes and E edges. Given Q queries, with each query indicating a starting node. The task is to print the sum of the distances from a given starting node S to every leaf node in the Weighted Tree.Examples: Input: N = 5, E = 4, Q = 3 1 (4) / \ (2) / \ 4 2 (5)/ \ (3) / \ 5 3Query 1: S = 1 Query 2: S = 3 Query
15 min read
Queries to calculate sum of the path from root to a given node in given Binary Tree
Given an infinite complete binary tree rooted at node 1, where every ith node has two children, with values 2 * i and 2 * (i + 1). Given another array arr[] consisting of N positive integers, the task for each array element arr[i] is to find the sum of the node values that occur in a path from the root node to the node arr[i]. Examples: Input: arr[
10 min read
Convert an arbitrary Binary Tree to a tree that holds Children Sum Property - Set 2
Question: Given an arbitrary binary tree, convert it to a binary tree that holds Children Sum Property. You can only increment data values in any node (You cannot change the structure of the tree and cannot decrement the value of any node). For example, the below tree doesn’t hold the children's sum property, convert it to a tree that holds the pro
9 min read
Convert an arbitrary Binary Tree to a tree that holds Children Sum Property
Question: Given an arbitrary binary tree, convert it to a binary tree that holds Children Sum Property. You can only increment data values in any node (You cannot change the structure of the tree and cannot decrement the value of any node). For example, the below tree doesn't hold the children sum property, convert it to a tree that holds the prope
15+ min read
Node having maximum sum of immediate children and itself in n-ary tree
Given an N-Ary tree, find and return the node for which sum of data of all children and the node itself is maximum. In the sum, data of node itself and data of its immediate children is to be taken. For example in the given tree, maxSum Node = 4 with maximum sum of 28 The idea is we will maintain a integer variable maxsum which contains the maximum
8 min read
Path from the root node to a given node in an N-ary Tree
Given an integer N and an N-ary Tree of the following form: Every node is numbered sequentially, starting from 1, till the last level, which contains the node N.The nodes at every odd level contains 2 children and nodes at every even level contains 4 children. The task is to print the path from the root node to the node N. Examples: Input: N = 14 O
10 min read
Number of children of given node in n-ary Tree
Given a node x, find the number of children of x(if it exists) in the given n-ary tree. Example : Input : x = 50 Output : 3 Explanation : 50 has 3 children having values 40, 100 and 20. Approach : Initialize the number of children as 0.For every node in the n-ary tree, check if its value is equal to x or not. If yes, then return the number of child
7 min read
Second unique smallest value of given Binary Tree whose each node is minimum of its children
Given a full binary tree where each node value is the same as the minimum value between its children, the task is to find the second minimum unique value of the tree. Examples: Input: tree: Output: 5Explanation: All the unique values present in the tree are 2, 5 and 7.The second smallest is 5. Input: tree: Output: -1Explanation: All the numbers pre
15+ min read
Find if there is a pair in root to a leaf path with sum equals to root's data
Given a binary tree, find if there is a pair in root to a leaf path such that sum of values in pair is equal to root's data. For example, in below tree there are no pairs in any root to leaf path with sum equal to root's data. The idea is based on hashing and tree traversal. The idea is similar to method 2 of array pair sum problem. Create an empty
8 min read
Digital Root (repeated digital sum) of square of an integer using Digital root of the given integer
Given an integer N, the task is to find the digital root N2 using the digital root of N. Digital Root of a positive integer is calculated by adding the digits of the integer. If the resultant value is a single digit, then that digit is the digital root. If the resultant value contains two or more digits, those digits are summed and the process is r
6 min read
Sum of nodes in the path from root to N-th node in given Tree
Given an integer N which needs to be present as a value in a node in the last level of a Tree rooted at 1 having nodes numbered from root to the last level by increments of 1. The nodes at every odd level contain 2 children and nodes at every even level contains 4 children. The task is to find the sum of node values in the path from the root to the
11 min read
Check if every node can me made accessible from a node of a Tree by at most N/2 given operations
Given a Directed Tree consisting of N nodes, the task is to check if there exists a node in the given tree such that all other nodes are reachable by removing any directed edge from the Tree and adding another directed edge between any pair of nodes in the Tree at most floor(N/2) times. If there exists any such node, then print "Yes". Otherwise, pr
6 min read
Find distance from root to given node in a binary tree
Given the root of a binary tree and a key x in it, find the distance of the given key from the root. Dis­tance means the num­ber of edges between two nodes. Examples: Input : x = 45, Root of below tree 5 / \ 10 15 / \ / \ 20 25 30 35 \ 45Output : Distance = 3 There are three edges on pathfrom root to 45.For more understanding of question,in above t
11 min read
Maximum XOR with given value in the path from root to given node in the tree
Given a tree with N distinct nodes from the range [1, n] and two integers x and val. The task is to find the maximum value of any node when XORed with x on the path from the root to val. Examples: Input: val = 6, x = 4 1 / \ 2 3 / \ 4 5 / 6 Output: 7 the path is 1 -> 3 -> 5 -> 6 1 ^ 4 = 5 3 ^ 4 = 7 5 ^ 4 = 1 6 ^ 4 = 2 Maximum is 7 Input: v
9 min read
Distance of each node of a Binary Tree from the root node using BFS
Given a Binary tree consisting of N nodes with values in the range [1, N], the task is to find the distance from the root node to every node of the tree. Examples: Input: 1 / \ 2 3 / \ \ 4 5 6 Output: 0 1 1 2 2 2 Explanation: The distance from the root to node 1 is 0. The distance from the root to node 2 is 1. The distance from the root to node 3 i
9 min read
General Tree (Each node can have arbitrary number of children) Level Order Traversal
Given a generic tree, perform a Level order traversal and print all of its nodes Examples: Input : 10 / / \ \ 2 34 56 100 / \ | / | \ 77 88 1 7 8 9 Output : 10 2 34 56 100 77 88 1 7 8 9 Input : 1 / / \ \ 2 3 4 5 / \ | / | \ 6 7 8 9 10 11 Output : 1 2 3 4 5 6 7 8 9 10 11 The approach to this problem is similar to Level Order traversal in a binary tr
7 min read
Sum of subtree depths for every node of a given Binary Tree
Given a binary tree consisting of N nodes, the task is to find the sum of depths of all subtree nodes in a given binary tree. Examples: Input: Output: 26Explanation: The leaf nodes having value 8, 9, 5, 6, and 7 have the sum of subtree depths equal to 0.Node 4 has a total of 2 nodes in its subtree, both in the same level. Therefore, sum of subtree
15+ min read
Smallest root of the equation x^2 + s(x)*x - n = 0, where s(x) is the sum of digits of root x.
You are given an integer n, find the smallest positive integer root of equation x, or else print -1 if no roots are found.Equation: x^2 + s(x)*x - n = 0where x, n are positive integers, s(x) is the function, equal to the sum of digits of number x in the decimal number system. 1 <= N <= 10^18 Examples: Input: N = 110 Output: 10 Explanation: x
8 min read
Maximize sum of path from the Root to a Leaf node in N-ary Tree
Given a generic tree consisting of N nodes, the task is to find the maximum sum of the path from the root to the leaf node. Examples: Input: Output: 12Explanation: The path sum to every leaf from the root are:For node 4: 1 -> 2 -> 4 = 7For node 5: 1 -> 2 -> 5 = 8For node 6: 1 -> 3 -> 6 = 10For node 7: 1 -> 3 -> 7 = 11For nod
6 min read
Maximum parent children sum in Binary tree
Given a Binary Tree, find the maximum sum in a binary tree by adding the parent with its children. Exactly three Node needs to be added. If the tree does not have a node with both of its children as not NULL, return 0. We simply traverse the tree and find the Node that has the maximum sum. We need to take care of the leaves. Implementation: C/C++ C
5 min read
Iterative approach to check for children sum property in a Binary Tree
Given a binary tree, write a function that returns true if the tree satisfies below property:For every node, data value must be equal to the sum of data values in left and right children. Consider data value as 0 for NULL children.Examples: Input : 10 / \ 8 2 / \ \ 3 5 2 Output : Yes Input : 5 / \ -2 7 / \ \ 1 6 7 Output : No We have already discus
8 min read
Check for Children Sum Property in a Binary Tree
Given a binary tree, the task is to check for every node, its value is equal to the sum of values of its immediate left and right child. For NULL values, consider the value to be 0. Example: Input: Output: The given tree satisfies the children sum property Recommended PracticeChildren Sum ParentTry It!Check for Children Sum Property in a Binary Tre
15+ min read
Create Binary Tree with Nodes as Cumulative sum of children
Given a root of a Binary Tree, the task is to update each of its nodes with the sum of all the nodes below it (from its left and right subtree), inclusive of the current Node. Example: Approach: The idea is to simply traverse through left and right subtree, and calculate sum of Left Subtree & Right Subtree, and then update the current node with
7 min read
Print Head node of every node in Binary Tree
Given the root of the Binary Tree. Print the parent node of each node in the given Binary Tree. Examples : Input: Binary Tree = {1, 2, 3, 4, 5, NULL, NULL, NULL, 6} Output : The Head Node of 6 is 4 The Head Nodeof 5 is 2The Head Node of 4 is 2The Head Node of 3 is 1 The Head Node of 2 is 1 Explanation: Clearly By Observing Image we are finding pare
7 min read
Sort the path from root to a given node in a Binary Tree
Given a Binary tree, the task is to sort the particular path from to a given node of the binary tree. You are given a Key Node and Tree. The task is to sort the path till that particular node. Examples: Input : 3 / \ 4 5 / \ \ 1 2 6 key = 2 Output : 2 / \ 3 5 / \ \ 1 4 6 Inorder :- 1 3 4 2 5 6 Here the path from root to given key is sorted from 3(r
6 min read
Print path from a node to root of given Complete Binary Tree
Given an integer N, the task is to find the path from the Nth node to the root of a Binary Tree of the following form: The Binary Tree is a Complete Binary Tree up to the level of the Nth node.The nodes are numbered 1 to N, starting from the root as 1.The structure of the Tree is as follows: 1 / \ 2 3 / \ / \ 4 5 6 7 ................ / \ ..........
4 min read
Print path from root to a given node in a binary tree
Given a binary tree with distinct nodes(no two nodes have the same data values). The problem is to print the path from root to a given node x. If node x is not present then print "No Path". Examples: Input : 1 / \ 2 3 / \ / \ 4 5 6 7 x = 5Output : 1->2->5 Approach: Create a recursive function that traverses the different path in the binary tr
15 min read
Print List of nodes of given n-ary Tree with number of children in range [0, n]
Given an n-ary Tree having N nodes numbered from 1 to N, the task is to print a list of nodes containing 0, 1, 2, 3, . . ., n children. Note: An n-ary Tree is a tree in which nodes can have at most n children. Examples: Input: Output: 0 child: 3, 5, 6, 71 child: 42 child: 23 child: 1 Input: Output: 0 child: 15, 30, 40, 99, 22, 46, 56, 25, 901 child
8 min read
Article Tags :
Practice Tags :