Last Updated: 9 Oct, 2020

Children Sum Property

Moderate
Asked in companies
AmazonQualcommSamsung

Problem statement

Given a binary tree of nodes 'N', you need to modify the value of its nodes, such that the tree holds the Children sum property.

A binary tree is said to follow the children sum property if, for every node of that tree, the value of that node is equal to the sum of the value(s) of all of its children nodes( left child and the right child).

Note :
 1. You can only increment the value of the nodes, in other words, the modified value must be at least equal to the original value of that node.
 2. You can not change the structure of the original binary tree.
 3. A binary tree is a tree in which each node has at most two children.      
 4. You can assume the value can be 0 for a NULL node and there can also be an empty tree.
Input Format :
The first line contains a single integer 'T' representing the number of test cases. 

The first and the only line of each test case will contain the values of the nodes of the tree in the level order form ( -1 for NULL node) Refer to the example for further clarification.
Example : 
Consider the binary tree :

abcd

The input of the tree depicted in the image above will be like : 

1
2 3
4 -1 5 6
-1 7 -1 -1 -1 -1
-1 -1

Explanation :
Level 1 :
The root node of the tree is 1

Level 2 :
Left child of 1 = 2
Right child of 1 = 3

Level 3 :
Left child of 2 = 4
Right child of 2 = null (-1)
Left child of 3 = 5
Right child of 3 = 6

Level 4 :
Left child of 4 = null (-1)
Right child of 4 = 7
Left child of 5 = null (-1)
Right child of 5 = null (-1)
Left child of 6 = null (-1)
Right child of 6 = null (-1)

Level 5 :
Left child of 7 = null (-1)
Right child of 7 = null (-1)

The first not-null node (of the previous level) is treated as the parent of the first two nodes of the current level. The second not-null node (of the previous level) is treated as the parent node for the next two nodes of the current level and so on.

The input ends when all nodes at the last level are null (-1).

##### Note : The above format was just to provide clarity on how the input is formed for a given tree. The sequence will be put together in a single line separated by a single space. Hence, for the above-depicted tree, the input will be given as:

1 2 3 4 -1 5 6 -1 7 -1 -1 -1 -1 -1 -1
Output Format :
For each test case, you just need to update the given tree in-place. If the updated tree satisfies all the conditions, the output will be shown as “Valid”, else the output will be “Invalid”.

The output of each test case will be printed in a separate line.
Note :
You do not need to print anything, it has already been taken care of. Just implement the given function.
Constraints :
1 <= T <= 10^2
0 <= N <= 10^2
1 <= node.Value <= 10^6

Time Limit : 1 sec

Approaches

01 Approach

This problem can be solved using a simple depth-first search, where the parent node is updated after its children.

 

  1. Let ‘parentVal’ be the value at the parent’s node and ‘childVal’ be the sum of the values of children nodes. Now there arise three cases :
  2. If ‘parentVal’ = ‘childVal’, then we have to do nothing.
  3. If ‘parentVal’ < ‘childVal’, then we can simply increase ‘parentVal’ by diff = ‘childVal’ - ‘parentVal’.
  4. If ‘parentVal’ > ‘childVal’, then we can’t decrease the value in a node, so this case is a bit tricky. To handle this case, we need to increase ‘childVal’ i.e increase the sum of children nodes and for that increase the values in the child nodes.
  5. To handle the last case, what we can do is traverse recursively either the left child or the right child,  as when incrementing a child’s value, it will violate its children sum property so we have to recursively update the values in the subtree also. If one child is ‘NULL’, then we can recursively update the second child.

02 Approach

The approach is simple, we will find the difference between the root node and the child nodes. If the difference is positive then we will add the difference to one of the child node and call this recursive function for both left and right child nodes and finally update the value of the root node equal to the sum of the left and right child node.

 

The steps are as follows:

  1. Take a variable diff to store the difference between the root and the sum of child nodes.
  2. If diff is positive then add the difference to any one of the child nodes.
  3. Call this function again for the left and right nodes to check whether the updated child nodes are following this property or not.
  4. Now update the current node as the sum of left and right child data.