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. Also, leaves are considered to follow the property.
Example:
Input:
Output: 1 Explanation: The above binary tree follows the Children Sum Property.
Input:
Output: 0 Explanation: The above binary tree doesn't follows the Children Sum Property as 5 + 2 != 8
[Expected Approach] Using Recursion - O(n) Time and O(h) Space:
The idea is traverse the binary tree recursively and check if the root node and its children satisfy the children sum property, which states that a node's value should equal the sum of its left and right children's values. The function checks if the current node's value matches this sum and recursively checks for both the subtrees.
Below is the implementation of this approach:
C++
// C++ Program to check children sum property#include<bits/stdc++.h>usingnamespacestd;classNode{public:intdata;Node*left;Node*right;Node(intx){data=x;left=nullptr;right=nullptr;}};// returns 1 if children sum property holds// for the given node and both of its childrenintisSumProperty(Node*root){// If root is NULL or it's a leaf node// then return trueif(root==nullptr||(root->left==nullptr&&root->right==nullptr))return1;intsum=0;// If left child is not present then 0// is used as data of left childif(root->left!=nullptr)sum+=root->left->data;// If right child is not present then 0// is used as data of right childif(root->right!=nullptr)sum+=root->right->data;// if the node and both of its children// satisfy the property return 1 else 0return((root->data==sum)&&isSumProperty(root->left)&&isSumProperty(root->right));}intmain(){// Create a hard coded tree.// 35// / \ // 20 15// / \ / \ // 15 5 10 5Node*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);cout<<isSumProperty(root)<<endl;return0;}
C
// C Program to check children sum property#include<stdio.h>#include<stdlib.h>structNode{intdata;structNode*left;structNode*right;};// returns 1 if children sum property holds// for the given node and both of its childrenintisSumProperty(structNode*root){// If root is NULL or it's a leaf node// then return trueif(root==NULL||(root->left==NULL&&root->right==NULL))return1;intsum=0;// If left child is not present then 0// is used as data of left childif(root->left!=NULL)sum+=root->left->data;// If right child is not present then 0// is used as data of right childif(root->right!=NULL)sum+=root->right->data;// if the node and both of its children// satisfy the property return 1 else 0return((root->data==sum)&&isSumProperty(root->left)&&isSumProperty(root->right));}structNode*createNode(intx){structNode*node=(structNode*)malloc(sizeof(structNode));node->data=x;node->left=NULL;node->right=NULL;returnnode;}intmain(){// Create a hard coded tree.// 35// / \ // 20 15// / \ / \ // 15 5 10 5structNode*root=createNode(35);root->left=createNode(20);root->right=createNode(15);root->left->left=createNode(15);root->left->right=createNode(5);root->right->left=createNode(10);root->right->right=createNode(5);printf("%d\n",isSumProperty(root));return0;}
Java
// java Program to check children sum propertyclassNode{intdata;Nodeleft,right;Node(intx){data=x;left=right=null;}}classGfG{// returns 1 if children sum property holds// for the given node and both of its childrenstaticintisSumProperty(Noderoot){// If root is NULL or it's a leaf node// then return trueif(root==null||(root.left==null&&root.right==null))return1;intsum=0;// If left child is not present then 0// is used as data of left childif(root.left!=null)sum+=root.left.data;// If right child is not present then 0// is used as data of right childif(root.right!=null)sum+=root.right.data;// if the node and both of its children// satisfy the property return 1 else 0return((root.data==sum)&&(isSumProperty(root.left)==1)&&(isSumProperty(root.right)==1))?1:0;}publicstaticvoidmain(String[]args){// Create a hard coded tree.// 35// / \// 20 15// / \ / \// 15 5 10 5Noderoot=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);System.out.println(isSumProperty(root));}}
Python
# Python Program to check children sum propertyclassNode:def__init__(self,x):self.data=xself.left=Noneself.right=NonedefisSumProperty(root):# If root is None or it's a leaf node# then return trueifrootisNoneor(root.leftisNoneandroot.rightisNone):return1sum=0# If left child is not present then 0# is used as data of left childifroot.leftisnotNone:sum+=root.left.data# If right child is not present then 0# is used as data of right childifroot.rightisnotNone:sum+=root.right.data# if the node and both of its children# satisfy the property return 1 else 0return1if(root.data==sumandisSumProperty(root.left)andisSumProperty(root.right))else0if__name__=="__main__":# Create a hard coded tree.# 35# / \# 20 15# / \ / \# 15 5 10 5root=Node(35)root.left=Node(20)root.right=Node(15)root.left.left=Node(15)root.left.right=Node(5)root.right.left=Node(10)root.right.right=Node(5)print(isSumProperty(root))
C#
// C# Program to check children sum propertyusingSystem;classNode{publicintdata;publicNodeleft,right;publicNode(intx){data=x;left=right=null;}}classGfG{// returns 1 if children sum property holds// for the given node and both of its childrenstaticintisSumProperty(Noderoot){// If root is NULL or it's a leaf node// then return trueif(root==null||(root.left==null&&root.right==null))return1;intsum=0;// If left child is not present then 0// is used as data of left childif(root.left!=null)sum+=root.left.data;// If right child is not present then 0// is used as data of right childif(root.right!=null)sum+=root.right.data;// if the node and both of its children// satisfy the property return 1 else 0return((root.data==sum)&&(isSumProperty(root.left)==1)&&(isSumProperty(root.right)==1))?1:0;}staticvoidMain(String[]args){// Create a hard coded tree.// 35// / \// 20 15// / \ / \// 15 5 10 5Noderoot=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);Console.WriteLine(isSumProperty(root));}}
JavaScript
// JavaScript Program to check children sum propertyclassNode{constructor(x){this.data=x;this.left=null;this.right=null;}}// returns 1 if children sum property holds// for the given node and both of its childrenfunctionisSumProperty(root){// If root is NULL or it's a leaf node// then return trueif(root===null||(root.left===null&&root.right===null))return1;letsum=0;// If left child is not present then 0// is used as data of left childif(root.left!==null)sum+=root.left.data;// If right child is not present then 0// is used as data of right childif(root.right!==null)sum+=root.right.data;// if the node and both of its children// satisfy the property return 1 else 0return(root.data===sum&&(isSumProperty(root.left)===1)&&(isSumProperty(root.right)===1))?1:0;}// Create a hard coded tree.// 35// / \// 20 15// / \ / \// 15 5 10 5letroot=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);console.log(isSumProperty(root));
Output
1
Time Complexity: O(n), where n is the number of nodes in the tree. Auxiliary Space: O(h), where h is the height of the tree.
[Alternate Approach] Using Queue - O(n) Time and O(n) Space:
The idea is to traverse the tree using level order approach. For each node, check if it satisfies children sum property. If it does, then push its children nodes into the queue. Otherwise return 0.
Below is the implementation of this approach:
C++
// C++ Program to check children sum property#include<bits/stdc++.h>usingnamespacestd;classNode{public:intdata;Node*left;Node*right;Node(intx){data=x;left=nullptr;right=nullptr;}};// returns 1 if children sum property holds// for the given node and both of its childrenintisSumProperty(Node*root){// If root is NULL// then return trueif(root==nullptr)return1;queue<Node*>q;q.push(root);while(!q.empty()){Node*curr=q.front();q.pop();// if this node is a leaf node, // then continueif(curr->left==nullptr&&curr->right==nullptr)continue;intsum=0;// If left child is not present then 0// is used as data of left childif(curr->left!=nullptr){sum+=curr->left->data;}// If right child is not present then 0// is used as data of right childif(curr->right!=nullptr){sum+=curr->right->data;}// if current node does not// follow the property, then// return 0.if(curr->data!=sum)return0;// Push the left child nodeif(curr->left!=nullptr)q.push(curr->left);// Push the right child nodeif(curr->right!=nullptr)q.push(curr->right);}return1;}intmain(){// Create a hard coded tree.// 35// / \ // 20 15// / \ / \ // 15 5 10 5Node*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);cout<<isSumProperty(root)<<endl;return0;}
Java
// Java Program to check children sum propertyimportjava.util.LinkedList;importjava.util.Queue;classNode{intdata;Nodeleft,right;Node(intx){data=x;left=right=null;}}classGfG{// returns 1 if children sum property holds// for the given node and both of its childrenstaticintisSumProperty(Noderoot){// If root is NULL// then return trueif(root==null)return1;Queue<Node>q=newLinkedList<>();q.add(root);while(!q.isEmpty()){Nodecurr=q.poll();// if this node is a leaf node, // then continueif(curr.left==null&&curr.right==null)continue;intsum=0;// If left child is not present then 0// is used as data of left childif(curr.left!=null){sum+=curr.left.data;}// If right child is not present then 0// is used as data of right childif(curr.right!=null){sum+=curr.right.data;}// if current node does not// follow the property, then// return 0.if(curr.data!=sum)return0;// Push the left child nodeif(curr.left!=null)q.add(curr.left);// Push the right child nodeif(curr.right!=null)q.add(curr.right);}return1;}publicstaticvoidmain(String[]args){// Create a hard coded tree.// 35// / \// 20 15// / \ / \// 15 5 10 5Noderoot=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);System.out.println(isSumProperty(root));}}
Python
# Python Program to check children sum propertyfromcollectionsimportdequeclassNode:def__init__(self,x):self.data=xself.left=Noneself.right=Nonedefis_sum_property(root):# If root is None# then return trueifrootisNone:return1q=deque([root])whileq:curr=q.popleft()# if this node is a leaf node, # then continueifcurr.leftisNoneandcurr.rightisNone:continuesum_val=0# If left child is not present then 0# is used as data of left childifcurr.leftisnotNone:sum_val+=curr.left.data# If right child is not present then 0# is used as data of right childifcurr.rightisnotNone:sum_val+=curr.right.data# if current node does not# follow the property, then# return 0.ifcurr.data!=sum_val:return0# Push the left child nodeifcurr.leftisnotNone:q.append(curr.left)# Push the right child nodeifcurr.rightisnotNone:q.append(curr.right)return1if__name__=="__main__":# Create a hard coded tree.# 35# / \# 20 15# / \ / \# 15 5 10 5root=Node(35)root.left=Node(20)root.right=Node(15)root.left.left=Node(15)root.left.right=Node(5)root.right.left=Node(10)root.right.right=Node(5)print(is_sum_property(root))
C#
// C# Program to check children sum propertyusingSystem;usingSystem.Collections.Generic;classNode{publicintdata;publicNodeleft,right;publicNode(intx){data=x;left=right=null;}}classGfG{// returns 1 if children sum property holds// for the given node and both of its childrenstaticintisSumProperty(Noderoot){// If root is NULL// then return trueif(root==null)return1;Queue<Node>q=newQueue<Node>();q.Enqueue(root);while(q.Count>0){Nodecurr=q.Dequeue();// if this node is a leaf node, // then continueif(curr.left==null&&curr.right==null)continue;intsum=0;// If left child is not present then 0// is used as data of left childif(curr.left!=null){sum+=curr.left.data;}// If right child is not present then 0// is used as data of right childif(curr.right!=null){sum+=curr.right.data;}// if current node does not// follow the property, then// return 0.if(curr.data!=sum)return0;// Push the left child nodeif(curr.left!=null)q.Enqueue(curr.left);// Push the right child nodeif(curr.right!=null)q.Enqueue(curr.right);}return1;}staticvoidMain(string[]args){// Create a hard coded tree.// 35// / \// 20 15// / \ / \// 15 5 10 5Noderoot=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);Console.WriteLine(isSumProperty(root));}}
JavaScript
// JavaScript Program to check children sum propertyclassNode{constructor(x){this.data=x;this.left=null;this.right=null;}}// returns 1 if children sum property holds// for the given node and both of its childrenfunctionisSumProperty(root){// If root is NULL// then return trueif(root===null)return1;letq=[];q.push(root);while(q.length>0){letcurr=q.shift();// if this node is a leaf node, // then continueif(curr.left===null&&curr.right===null)continue;letsum=0;// If left child is not present then 0// is used as data of left childif(curr.left!==null){sum+=curr.left.data;}// If right child is not present then 0// is used as data of right childif(curr.right!==null){sum+=curr.right.data;}// if current node does not// follow the property, then// return 0.if(curr.data!==sum)return0;// Push the left child nodeif(curr.left!==null)q.push(curr.left);// Push the right child nodeif(curr.right!==null)q.push(curr.right);}return1;}// Create a hard coded tree.// 35// / \// 20 15// / \ / \// 15 5 10 5letroot=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);console.log(isSumProperty(root));
Output
1
Time Complexity: O(n), where n is the number of nodes in the tree. Auxiliary Space: O(n)
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.