Iterative approach to check for children sum property in a Binary Tree
Last Updated :
06 Aug, 2021
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 discussed the recursive approach. In this post an iterative approach is discussed.
Approach: The idea is to use a queue to do level order traversal of the Binary Tree and simultaneously check for every node:
- If the current node has two children and if the current node is equal to the sum of its left and right children.
- If the current node has just left child and if the current node is equal to its left child.
- If the current node has just right child and if the current node is equal to its right child.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
Node *left, *right;
};
Node* newNode( int data)
{
Node* node = new (Node);
node->data = data;
node->left = node->right = NULL;
return (node);
}
bool CheckChildrenSum(Node* root)
{
queue<Node*> q;
q.push(root);
while (!q.empty()) {
Node* temp = q.front();
q.pop();
if (temp->left && temp->right) {
if (temp->data != temp->left->data + temp->right->data)
return false ;
q.push(temp->left);
q.push(temp->right);
}
else if (!temp->left && temp->right) {
if (temp->data != temp->right->data)
return false ;
q.push(temp->right);
}
else if (!temp->right && temp->left) {
if (temp->data != temp->left->data)
return false ;
q.push(temp->left);
}
}
return true ;
}
int main()
{
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 (CheckChildrenSum(root))
printf ( "Yes" );
else
printf ( "No" );
return 0;
}
|
Java
import java.util.*;
class GFG
{
static class Node
{
int data;
Node left, right;
}
static Node newNode( int data)
{
Node node = new Node();
node.data = data;
node.left = node.right = null ;
return (node);
}
static boolean CheckChildrenSum(Node root)
{
Queue<Node> q = new LinkedList<Node>();
q.add(root);
while (q.size() > 0 )
{
Node temp = q.peek();
q.remove();
if (temp.left != null && temp.right != null )
{
if (temp.data != temp.left.data + temp.right.data)
return false ;
q.add(temp.left);
q.add(temp.right);
}
else if (temp.left == null && temp.right != null )
{
if (temp.data != temp.right.data)
return false ;
q.add(temp.right);
}
else if (temp.right == null && temp.left != null )
{
if (temp.data != temp.left.data)
return false ;
q.add(temp.left);
}
}
return true ;
}
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 (CheckChildrenSum(root))
System.out.printf( "Yes" );
else
System.out.printf( "No" );
}
}
|
Python3
class Node:
def __init__( self , data):
self .data = data
self .left = None
self .right = None
def CheckChildrenSum(root):
q = []
q.append(root)
while len (q) ! = 0 :
temp = q.pop()
if temp.left and temp.right:
if (temp.data ! = temp.left.data +
temp.right.data):
return False
q.append(temp.left)
q.append(temp.right)
elif not temp.left and temp.right:
if temp.data ! = temp.right.data:
return False
q.append(temp.right)
elif not temp.right and temp.left:
if temp.data ! = temp.left.data:
return False
q.append(temp.left)
return True
if __name__ = = "__main__" :
root = Node( 10 )
root.left = Node( 8 )
root.right = Node( 2 )
root.left.left = Node( 3 )
root.left.right = Node( 5 )
root.right.right = Node( 2 )
if CheckChildrenSum(root):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
using System.Collections.Generic;
class GFG
{
public class Node
{
public int data;
public Node left, right;
}
static Node newNode( int data)
{
Node node = new Node();
node.data = data;
node.left = node.right = null ;
return (node);
}
static Boolean CheckChildrenSum(Node root)
{
Queue<Node> q = new Queue<Node>();
q.Enqueue(root);
while (q.Count > 0)
{
Node temp = q.Peek();
q.Dequeue();
if (temp.left != null &&
temp.right != null )
{
if (temp.data != temp.left.data +
temp.right.data)
return false ;
q.Enqueue(temp.left);
q.Enqueue(temp.right);
}
else if (temp.left == null &&
temp.right != null )
{
if (temp.data != temp.right.data)
return false ;
q.Enqueue(temp.right);
}
else if (temp.right == null &&
temp.left != null )
{
if (temp.data != temp.left.data)
return false ;
q.Enqueue(temp.left);
}
}
return true ;
}
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 (CheckChildrenSum(root))
Console.WriteLine( "Yes" );
else
Console.WriteLine( "No" );
}
}
|
Javascript
<script>
class Node
{
constructor(data) {
this .left = null ;
this .right = null ;
this .data = data;
}
}
function newNode(data)
{
let node = new Node(data);
return (node);
}
function CheckChildrenSum(root)
{
let q = [];
q.push(root);
while (q.length > 0)
{
let temp = q[0];
q.shift();
if (temp.left != null && temp.right != null )
{
if (temp.data != temp.left.data + temp.right.data)
return false ;
q.push(temp.left);
q.push(temp.right);
}
else if (temp.left == null && temp.right != null )
{
if (temp.data != temp.right.data)
return false ;
q.push(temp.right);
}
else if (temp.right == null && temp.left != null )
{
if (temp.data != temp.left.data)
return false ;
q.push(temp.left);
}
}
return true ;
}
let 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 (CheckChildrenSum(root))
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Time Complexity: O(N), where N is the total number of nodes in the binary tree.
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...