Given a binary tree, find if it satisfies theChildren Sum Property which has the following rules
Each non-leaf node must have a value equal to the sum of its left and right children's values.
A NULL child is considered to have a value of 0, and all leaf nodes are considered valid by default.
Examples:
Input: root = [35, 20, 15, 15, 5, 10, 5]
Output: true Explanation: Here, every node is sum of its left and right child.
Input: root = [1, 4, 3, 5]
Output: false Explanation: Here, 1 is the root node and 4, 3 are its child nodes. 4 + 3 = 7 which is not equal to the value of root node. Hence, this tree does not satisfy the given condition.
Using Recursive Validation - O(n) Time and O(h) Space
The idea is to use recursion to check the Children Sum Property while traversing the tree. For every node, we compare its value with the sum of its children.
Working of Approach:
Start from the root node. If the node is NULL or a leaf node, it automatically satisfies the Children Sum Property.
For every non-leaf node, compute the sum of the left and right child values (consider 0 for a missing child).
Compare this sum with the current node's value. If they are equal, recursively check the left and right subtrees.
If every node satisfies the condition, return true; otherwise, return false as soon as any node violates the property.
Let us understand with an example: Input: root = [35, 20, 15, 15, 5, 10, 5]
Start from the root node (35) and check whether its value equals the sum of its children (20 + 15 = 35). Since it matches, recursively check both subtrees.
Move to node 20. Its children are 15 and 5, and 15 + 5 = 20, so continue checking its left and right child.
Nodes 15 and 5 are leaf nodes, so they automatically satisfy the Children Sum Property and return true.
Next, check node 15 on the right. Its children are 10 and 5, and 10 + 5 = 15, so both leaf nodes return true.
Since every non-leaf node satisfies the property, the function returns true.
C++
#include<bits/stdc++.h>usingnamespacestd;classNode{public:intdata;Node*left,*right;Node(intval){data=val;left=right=nullptr;}};boolisSumProperty(Node*root){intleft_data=0,right_data=0;// if node is null or both child nodes are null, we return true.if(root==nullptr||(root->left==nullptr&&root->right==nullptr))returntrue;else{// if left child is not null then we store its value.if(root->left!=nullptr)left_data=root->left->data;// if right child is not null then we store its value.if(root->right!=nullptr)right_data=root->right->data;// if sum of stored data of left and right child is equal to the current// node data and recursively for the left and right subtree, parent data// is equal to sum of child data then we return true.if((root->data==left_data+right_data)&&isSumProperty(root->left)&&isSumProperty(root->right))returntrue;// else we return false.elsereturnfalse;}}intmain(){/* 35 / \ 20 15 / \ / \ 15 5 10 5 */Node*root=newNode(35);root->left=newNode(20);root->right=newNode(15);root->left->left=newNode(15);root->left->right=newNode(5);root->right->left=newNode(10);root->right->right=newNode(5);if(isSumProperty(root))cout<<"true";elsecout<<"false";return0;}
Java
classNode{intdata;Nodeleft,right;Node(intval){data=val;left=right=null;}}classGFG{staticbooleanisSumProperty(Noderoot){intleft_data=0,right_data=0;// if node is null or both child nodes are null, we// return true.if(root==null||(root.left==null&&root.right==null))returntrue;else{// if left child is not null then we store its// value.if(root.left!=null)left_data=root.left.data;// if right child is not null then we store its// value.if(root.right!=null)right_data=root.right.data;// if sum of stored data of left and right child// is equal to the current node data and// recursively for the left and right subtree,// parent data is equal to sum of child data// then we return true.if((root.data==left_data+right_data)&&isSumProperty(root.left)&&isSumProperty(root.right))returntrue;// else we return false.elsereturnfalse;}}publicstaticvoidmain(String[]args){/* 35 / \ 20 15 / \ / \ 15 5 10 5 */Noderoot=newNode(35);root.left=newNode(20);root.right=newNode(15);root.left.left=newNode(15);root.left.right=newNode(5);root.right.left=newNode(10);root.right.right=newNode(5);if(isSumProperty(root))System.out.print("true");elseSystem.out.print("false");}}
usingSystem;classNode{publicintdata;publicNodeleft,right;publicNode(intval){data=val;left=right=null;}}classGFG{staticboolisSumProperty(Noderoot){intleft_data=0,right_data=0;// if node is null or both child nodes are null, we// return true.if(root==null||(root.left==null&&root.right==null))returntrue;else{// if left child is not null then we store its// value.if(root.left!=null)left_data=root.left.data;// if right child is not null then we store its// value.if(root.right!=null)right_data=root.right.data;// if sum of stored data of left and right child// is equal to the current node data and// recursively for the left and right subtree,// parent data is equal to sum of child data// then we return true.if((root.data==left_data+right_data)&&isSumProperty(root.left)&&isSumProperty(root.right))returntrue;// else we return false.elsereturnfalse;}}staticvoidMain(){/* 35 / \ 20 15 / \ / \ 15 5 10 5 */Noderoot=newNode(35);root.left=newNode(20);root.right=newNode(15);root.left.left=newNode(15);root.left.right=newNode(5);root.right.left=newNode(10);root.right.right=newNode(5);if(isSumProperty(root))Console.Write("true");elseConsole.Write("false");}}
JavaScript
classNode{constructor(val){this.data=val;this.left=null;this.right=null;}}functionisSumProperty(root){letleft_data=0,right_data=0;// if node is null or both child nodes are null, we// return true.if(root===null||(root.left===null&&root.right===null))returntrue;else{// if left child is not null then we store its// value.if(root.left!==null)left_data=root.left.data;// if right child is not null then we store its// value.if(root.right!==null)right_data=root.right.data;// if sum of stored data of left and right child is// equal to the current node data and recursively// for the left and right subtree, parent data is// equal to sum of child data then we return true.if((root.data===left_data+right_data)&&isSumProperty(root.left)&&isSumProperty(root.right))returntrue;// else we return false.elsereturnfalse;}}// Driver Codeletroot=newNode(35);root.left=newNode(20);root.right=newNode(15);root.left.left=newNode(15);root.left.right=newNode(5);root.right.left=newNode(10);root.right.right=newNode(5);if(isSumProperty(root))console.log("true");elseconsole.log("false");