Check for Children Sum Property in a Binary Tree
Last Updated :
15 Mar, 2023
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
Check for Children Sum Property in a Binary Tree using recursion:
To solve the problem follow the below idea:
Traverse the given binary tree. For each node check (recursively) if the node and both its children satisfy the Children Sum Property, if so then return true else return false
Below is the implementation of this approach:
C++
#include <bits/stdc++.h>
using namespace std;
struct node {
int data;
struct node* left;
struct node* right;
};
int isSumProperty( struct node* node)
{
int sum = 0;
if (node == NULL
|| (node->left == NULL && node->right == NULL))
return 1;
else {
if (node->left != NULL)
sum += node->left->data;
if (node->right != NULL)
sum += node->right->data;
return ((node->data == sum)
&& isSumProperty(node->left)
&& isSumProperty(node->right));
}
}
struct node* newNode( int data)
{
struct node* node
= ( struct node*) malloc ( sizeof ( struct node));
node->data = data;
node->left = NULL;
node->right = NULL;
return (node);
}
int main()
{
struct node* root = newNode(10);
root->left = newNode(8);
root->right = newNode(2);
root->left->left = newNode(3);
root->left->right = newNode(5);
root->right->right = newNode(2);
if (isSumProperty(root))
cout << "The given tree satisfies "
<< "the children sum property " ;
else
cout << "The given tree does not satisfy "
<< "the children sum property " ;
getchar ();
return 0;
}
|
C
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node* left;
struct node* right;
};
int isSumProperty( struct node* node)
{
int left_data = 0, right_data = 0;
if (node == NULL
|| (node->left == NULL && node->right == NULL))
return 1;
else {
if (node->left != NULL)
left_data = node->left->data;
if (node->right != NULL)
right_data = node->right->data;
if ((node->data == left_data + right_data)
&& isSumProperty(node->left)
&& isSumProperty(node->right))
return 1;
else
return 0;
}
}
struct node* newNode( int data)
{
struct node* node
= ( struct node*) malloc ( sizeof ( struct node));
node->data = data;
node->left = NULL;
node->right = NULL;
return (node);
}
int main()
{
struct node* root = newNode(10);
root->left = newNode(8);
root->right = newNode(2);
root->left->left = newNode(3);
root->left->right = newNode(5);
root->right->right = newNode(2);
if (isSumProperty(root))
printf ( "The given tree satisfies the children sum "
"property " );
else
printf ( "The given tree does not satisfy the "
"children sum property " );
getchar ();
return 0;
}
|
Java
class Node {
int data;
Node left, right;
public Node( int d)
{
data = d;
left = right = null ;
}
}
class BinaryTree {
Node root;
int isSumProperty(Node node)
{
int left_data = 0 , right_data = 0 ;
if (node == null
|| (node.left == null && node.right == null ))
return 1 ;
else {
if (node.left != null )
left_data = node.left.data;
if (node.right != null )
right_data = node.right.data;
if ((node.data == left_data + right_data)
&& (isSumProperty(node.left) != 0 )
&& isSumProperty(node.right) != 0 )
return 1 ;
else
return 0 ;
}
}
public static void main(String[] args)
{
BinaryTree tree = new BinaryTree();
tree.root = new Node( 10 );
tree.root.left = new Node( 8 );
tree.root.right = new Node( 2 );
tree.root.left.left = new Node( 3 );
tree.root.left.right = new Node( 5 );
tree.root.right.right = new Node( 2 );
if (tree.isSumProperty(tree.root) != 0 )
System.out.println(
"The given tree satisfies children"
+ " sum property" );
else
System.out.println(
"The given tree does not satisfy children"
+ " sum property" );
}
}
|
Python3
class newNode:
def __init__( self , data):
self .data = data
self .left = None
self .right = None
def isSumProperty(node):
left_data = 0
right_data = 0
if (node = = None or (node.left = = None and
node.right = = None )):
return 1
else :
if (node.left ! = None ):
left_data = node.left.data
if (node.right ! = None ):
right_data = node.right.data
if ((node.data = = left_data + right_data) and
isSumProperty(node.left) and
isSumProperty(node.right)):
return 1
else :
return 0
if __name__ = = '__main__' :
root = newNode( 10 )
root.left = newNode( 8 )
root.right = newNode( 2 )
root.left.left = newNode( 3 )
root.left.right = newNode( 5 )
root.right.right = newNode( 2 )
if (isSumProperty(root)):
print ( "The given tree satisfies the" ,
"children sum property " )
else :
print ( "The given tree does not satisfy" ,
"the children sum property " )
|
C#
using System;
public class Node {
public int data;
public Node left, right;
public Node( int d)
{
data = d;
left = right = null ;
}
}
class GFG {
public Node root;
public virtual int isSumProperty(Node node)
{
int left_data = 0, right_data = 0;
if (node == null
|| (node.left == null && node.right == null )) {
return 1;
}
else {
if (node.left != null ) {
left_data = node.left.data;
}
if (node.right != null ) {
right_data = node.right.data;
}
if ((node.data == left_data + right_data)
&& (isSumProperty(node.left) != 0)
&& isSumProperty(node.right) != 0) {
return 1;
}
else {
return 0;
}
}
}
public static void Main( string [] args)
{
GFG tree = new GFG();
tree.root = new Node(10);
tree.root.left = new Node(8);
tree.root.right = new Node(2);
tree.root.left.left = new Node(3);
tree.root.left.right = new Node(5);
tree.root.right.right = new Node(2);
if (tree.isSumProperty(tree.root) != 0) {
Console.WriteLine( "The given tree satisfies"
+ " children sum property" );
}
else {
Console.WriteLine(
"The given tree does not"
+ " satisfy children sum property" );
}
}
}
|
Javascript
<script>
class Node
{
constructor(data) {
this .left = null ;
this .right = null ;
this .data = data;
}
}
let root;
function isSumProperty(node)
{
let left_data = 0, right_data = 0;
if (node == null
|| (node.left == null && node.right == null ))
return 1;
else
{
if (node.left != null )
left_data = node.left.data;
if (node.right != null )
right_data = node.right.data;
if ((node.data == left_data + right_data)
&& (isSumProperty(node.left)!=0)
&& isSumProperty(node.right)!=0)
return 1;
else
return 0;
}
}
root = new Node(10);
root.left = new Node(8);
root.right = new Node(2);
root.left.left = new Node(3);
root.left.right = new Node(5);
root.right.right = new Node(2);
if (isSumProperty(root) != 0)
document.write( "The given tree satisfies the children"
+ " sum property" );
else
document.write( "The given tree does not satisfy children"
+ " sum property" );
</script>
|
Output
The given tree satisfies the children sum property
Time Complexity: O(N), we are doing a complete traversal of the tree.
Auxiliary Space: O(log N), Auxiliary stack space used by recursion calls
Check for Children Sum Property in a Binary Tree using deque:
Follow the level order traversal approach and while pushing each node->left and node->right, if they exist add their sum and check if equal to current node->data.
Below is the implementation of this approach:
C++
#include <bits/stdc++.h>
using namespace std;
struct node {
int data;
struct node* left;
struct node* right;
};
int isSumProperty(node *root)
{
queue<node*>q;
q.push(root);
q.push(NULL);
while (!q.empty())
{
node* curr=q.front();q.pop();
if (curr==NULL)
{
if (!q.empty())
q.push(NULL); continue ;
}
int sum=0;
if (curr->left)
{
q.push(curr->left);
sum+=curr->left->data;
}
if (curr->right)
{
q.push(curr->right);
sum+=curr->right->data;
}
if (sum!=curr->data&&sum!=0)
return 0;
}
return 1;
}
struct node* newNode( int data)
{
struct node* node
= ( struct node*) malloc ( sizeof ( struct node));
node->data = data;
node->left = NULL;
node->right = NULL;
return (node);
}
int main()
{
struct node* root = newNode(10);
root->left = newNode(8);
root->right = newNode(2);
root->left->left = newNode(3);
root->left->right = newNode(5);
root->right->right = newNode(2);
if (isSumProperty(root))
cout << "The given tree satisfies "
<< "the children sum property " ;
else
cout << "The given tree does not satisfy "
<< "the children sum property " ;
getchar ();
return 0;
}
|
Java
import java.util.LinkedList;
import java.util.Queue;
class Node {
int data;
Node left;
Node right;
Node( int value) {
data = value;
left = null ;
right = null ;
}
}
class Main {
static int isSumProperty(Node root) {
Queue<Node> q = new LinkedList<Node>();
q.offer(root);
q.offer( null );
while (!q.isEmpty()) {
Node curr = q.poll();
if (curr == null ) {
if (!q.isEmpty())
q.offer( null );
continue ;
}
int sum = 0 ;
if (curr.left != null ) {
q.offer(curr.left);
sum += curr.left.data;
}
if (curr.right != null ) {
q.offer(curr.right);
sum += curr.right.data;
}
if (sum != curr.data && sum != 0 )
return 0 ;
}
return 1 ;
}
static Node newNode( int data) {
Node node = new Node(data);
node.data = data;
node.left = null ;
node.right = null ;
return node;
}
public static void main(String[] args) {
Node root = newNode( 10 );
root.left = newNode( 8 );
root.right = newNode( 2 );
root.left.left = newNode( 3 );
root.left.right = newNode( 5 );
root.right.right = newNode( 2 );
if (isSumProperty(root) == 1 )
System.out.println( "The given tree satisfies the children sum property" );
else
System.out.println( "The given tree does not satisfy the children sum property" );
}
}
|
Python3
from queue import Queue
class newNode:
def __init__( self , data):
self .data = data
self .left = None
self .right = None
def isSumProperty(root):
q = Queue()
q.put(root)
q.put( None )
while not q.empty():
curr = q.get()
if curr = = None :
if not q.empty():
q.put( None )
continue
sum = 0
if curr.left:
q.put(curr.left)
sum + = curr.left.data
if curr.right:
q.put(curr.right)
sum + = curr.right.data
if sum ! = curr.data and sum ! = 0 :
return 0
return 1
if __name__ = = '__main__' :
root = newNode( 10 )
root.left = newNode( 8 )
root.right = newNode( 2 )
root.left.left = newNode( 3 )
root.left.right = newNode( 5 )
root.right.right = newNode( 2 )
if (isSumProperty(root)):
print ( "The given tree satisfies the" ,
"children sum property " )
else :
print ( "The given tree does not satisfy" ,
"the children sum property " )
|
C#
using System;
using System.Collections.Generic;
class Node {
public int data;
public Node left;
public Node right;
public Node( int value) {
data = value;
left = null ;
right = null ;
}
}
class GFG {
static int isSumProperty(Node root) {
Queue<Node> q = new Queue<Node>();
q.Enqueue(root);
q.Enqueue( null );
while (q.Count != 0) {
Node curr = q.Dequeue();
if (curr == null ) {
if (q.Count != 0) {
q.Enqueue( null );
}
continue ;
}
int sum = 0;
if (curr.left != null ) {
q.Enqueue(curr.left);
sum += curr.left.data;
}
if (curr.right != null ) {
q.Enqueue(curr.right);
sum += curr.right.data;
}
if (sum != curr.data && sum != 0) {
return 0;
}
}
return 1;
}
static Node newNode( int data) {
Node node = new Node(data);
node.data = data;
node.left = null ;
node.right = null ;
return node;
}
static void Main( string [] args) {
Node root = newNode(10);
root.left = newNode(8);
root.right = newNode(2);
root.left.left = newNode(3);
root.left.right = newNode(5);
root.right.right = newNode(2);
if (isSumProperty(root) == 1) {
Console.WriteLine( "The given tree satisfies the children sum property" );
}
else {
Console.WriteLine( "The given tree does not satisfy the children sum property" );
}
}
}
|
Javascript
class Node {
constructor(data) {
this .data = data;
this .left = null ;
this .right = null ;
}
}
function isSumProperty(root) {
let q = [];
q.push(root);
while (q.length > 0) {
let curr = q.shift();
let sum = 0;
if (curr.left) {
q.push(curr.left);
sum += curr.left.data;
}
if (curr.right) {
q.push(curr.right);
sum += curr.right.data;
}
if (sum != curr.data && sum != 0) {
return 0;
}
}
return 1;
}
let root = new Node(10);
root.left = new Node(8);
root.right = new Node(2);
root.left.left = new Node(3);
root.left.right = new Node(5);
root.right.right = new Node(2);
if (isSumProperty(root)) {
console.log( "The given tree satisfies the children sum property" );
} else {
console.log( "The given tree does not satisfy the children sum property" );
}
|
Output
The given tree satisfies the children sum property
Time Complexity: O(N), for complete traversal of the tree.
Auxiliary Space: O(N), for storing the nodes in the deque.
Share your thoughts in the comments
Please Login to comment...