Write a function that returns true if the given Binary Tree is SumTree else false. A SumTree is a Binary Tree where the value of a node is equal to the sum of the nodes present in its left subtree and right subtree. An empty tree is SumTree and sum of an empty tree can be considered as 0. A leaf node is also considered as SumTree.
Following is an example of SumTree.
26
/ \
10 3
/ \ \
4 6 3
Method 1 ( Simple )
Get the sum of nodes in the left subtree and right subtree. Check if the sum calculated is equal to the root’s data. Also, recursively check if the left and right subtrees are SumTrees.
C++
#include <iostream>
using namespace std;
struct node
{
int data;
struct node* left;
struct node* right;
};
int sum( struct node *root)
{
if (root == NULL)
return 0;
return sum(root->left) + root->data +
sum(root->right);
}
int isSumTree( struct node* node)
{
int ls, rs;
if (node == NULL ||
(node->left == NULL &&
node->right == NULL))
return 1;
ls = sum(node->left);
rs = sum(node->right);
if ((node->data == ls + rs) &&
isSumTree(node->left) &&
isSumTree(node->right))
return 1;
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(26);
root->left = newNode(10);
root->right = newNode(3);
root->left->left = newNode(4);
root->left->right = newNode(6);
root->right->right = newNode(3);
if (isSumTree(root))
cout << "The given tree is a SumTree " ;
else
cout << "The given tree is not a SumTree " ;
getchar ();
return 0;
}
|
C
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node* left;
struct node* right;
};
int sum( struct node *root)
{
if (root == NULL)
return 0;
return sum(root->left) + root->data + sum(root->right);
}
int isSumTree( struct node* node)
{
int ls, rs;
if (node == NULL ||
(node->left == NULL && node->right == NULL))
return 1;
ls = sum(node->left);
rs = sum(node->right);
if ((node->data == ls + rs)&&
isSumTree(node->left) &&
isSumTree(node->right))
return 1;
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(26);
root->left = newNode(10);
root->right = newNode(3);
root->left->left = newNode(4);
root->left->right = newNode(6);
root->right->right = newNode(3);
if (isSumTree(root))
printf ( "The given tree is a SumTree." );
else
printf ( "The given tree is not a SumTree." );
getchar ();
return 0;
}
|
Java
import java.io.*;
class Node
{
int data;
Node left, right, nextRight;
Node( int item)
{
data = item;
left = right = null ;
}
}
class BinaryTree {
public static Node root;
static int sum(Node node)
{
if (node == null )
{
return 0 ;
}
return (sum(node.left) + node.data+sum(node.right));
}
static int isSumTree(Node node)
{
int ls,rs;
if (node == null || (node.left == null && node.right == null ))
{
return 1 ;
}
ls = sum(node.left);
rs = sum(node.right);
if ((node.data == ls + rs) && isSumTree(node.left) != 0 && isSumTree(node.right) != 0 )
{
return 1 ;
}
return 0 ;
}
public static void main (String[] args)
{
BinaryTree tree= new BinaryTree();
tree.root= new Node( 26 );
tree.root.left= new Node( 10 );
tree.root.right= new Node( 3 );
tree.root.left.left= new Node( 4 );
tree.root.left.right= new Node( 6 );
tree.root.right.right= new Node( 3 );
if (isSumTree(root) != 0 )
{
System.out.println( "The given tree is a SumTree" );
}
else
{
System.out.println( "The given tree is not a SumTree" );
}
}
}
|
Python3
class node:
def __init__( self , x):
self .data = x
self .left = None
self .right = None
def sum (root):
if (root = = None ):
return 0
return ( sum (root.left) +
root.data +
sum (root.right))
def isSumTree(node):
if (node = = None or
(node.left = = None and
node.right = = None )):
return 1
ls = sum (node.left)
rs = sum (node.right)
if ((node.data = = ls + rs) and
isSumTree(node.left) and
isSumTree(node.right)):
return 1
return 0
if __name__ = = '__main__' :
root = node( 26 )
root.left = node( 10 )
root.right = node( 3 )
root.left.left = node( 4 )
root.left.right = node( 6 )
root.right.right = node( 3 )
if (isSumTree(root)):
print ( "The given tree is a SumTree " )
else :
print ( "The given tree is not a SumTree " )
|
C#
using System;
public class Node
{
public int data;
public Node left, right, nextRight;
public Node( int item)
{
data = item;
left = right = null ;
}
}
class BinaryTree {
public Node root;
int sum(Node node)
{
if (node == null )
{
return 0;
}
return (sum(node.left) + node.data+sum(node.right));
}
int isSumTree(Node node)
{
int ls,rs;
if (node == null || (node.left == null && node.right == null ))
{
return 1;
}
ls = sum(node.left);
rs = sum(node.right);
if ((node.data == ls + rs) && isSumTree(node.left) != 0 && isSumTree(node.right) != 0)
{
return 1;
}
return 0;
}
public static void Main( string [] args)
{
BinaryTree tree = new BinaryTree();
tree.root = new Node(26);
tree.root.left = new Node(10);
tree.root.right = new Node(3);
tree.root.left.left = new Node(4);
tree.root.left.right = new Node(6);
tree.root.right.right = new Node(3);
if (tree.isSumTree(tree.root) != 0)
{
Console.WriteLine( "The given tree is a SumTree" );
}
else
{
Console.WriteLine( "The given tree is not a SumTree" );
}
}
}
|
Output
The given tree is a SumTree
Time Complexity: O(n^2) in the worst case. The worst-case occurs for a skewed tree.
Method 2 ( Tricky )
Method 1 uses sum() to get the sum of nodes in left and right subtrees. Method 2 uses the following rules to get the sum directly.
1) If the node is a leaf node then the sum of the subtree rooted with this node is equal to the value of this node.
2) If the node is not a leaf node then the sum of the subtree rooted with this node is twice the value of this node (Assuming that the tree rooted with this node is SumTree).
C++
#include<bits/stdc++.h>
using namespace std;
struct node
{
int data;
node* left;
node* right;
};
int isLeaf(node *node)
{
if (node == NULL)
return 0;
if (node->left == NULL && node->right == NULL)
return 1;
return 0;
}
int isSumTree(node* node)
{
int ls;
int rs;
if (node == NULL || isLeaf(node))
return 1;
if ( isSumTree(node->left) && isSumTree(node->right))
{
if (node->left == NULL)
ls = 0;
else if (isLeaf(node->left))
ls = node->left->data;
else
ls = 2 * (node->left->data);
if (node->right == NULL)
rs = 0;
else if (isLeaf(node->right))
rs = node->right->data;
else
rs = 2 * (node->right->data);
return (node->data == ls + rs);
}
return 0;
}
node* newNode( int data)
{
node* node1 = new node();
node1->data = data;
node1->left = NULL;
node1->right = NULL;
return (node1);
}
int main()
{
node *root = newNode(26);
root->left = newNode(10);
root->right = newNode(3);
root->left->left = newNode(4);
root->left->right = newNode(6);
root->right->right = newNode(3);
if (isSumTree(root))
cout << "The given tree is a SumTree " ;
else
cout << "The given tree is not a SumTree " ;
return 0;
}
|
C
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node* left;
struct node* right;
};
int isLeaf( struct node *node)
{
if (node == NULL)
return 0;
if (node->left == NULL && node->right == NULL)
return 1;
return 0;
}
int isSumTree( struct node* node)
{
int ls;
int rs;
if (node == NULL || isLeaf(node))
return 1;
if ( isSumTree(node->left) && isSumTree(node->right))
{
if (node->left == NULL)
ls = 0;
else if (isLeaf(node->left))
ls = node->left->data;
else
ls = 2*(node->left->data);
if (node->right == NULL)
rs = 0;
else if (isLeaf(node->right))
rs = node->right->data;
else
rs = 2*(node->right->data);
return (node->data == ls + rs);
}
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(26);
root->left = newNode(10);
root->right = newNode(3);
root->left->left = newNode(4);
root->left->right = newNode(6);
root->right->right = newNode(3);
if (isSumTree(root))
printf ( "The given tree is a SumTree " );
else
printf ( "The given tree is not a SumTree " );
getchar ();
return 0;
}
|
Java
class Node
{
int data;
Node left, right, nextRight;
Node( int item)
{
data = item;
left = right = nextRight = null ;
}
}
class BinaryTree
{
Node root;
int isLeaf(Node node)
{
if (node == null )
return 0 ;
if (node.left == null && node.right == null )
return 1 ;
return 0 ;
}
int isSumTree(Node node)
{
int ls;
int rs;
if (node == null || isLeaf(node) == 1 )
return 1 ;
if (isSumTree(node.left) != 0 && isSumTree(node.right) != 0 )
{
if (node.left == null )
ls = 0 ;
else if (isLeaf(node.left) != 0 )
ls = node.left.data;
else
ls = 2 * (node.left.data);
if (node.right == null )
rs = 0 ;
else if (isLeaf(node.right) != 0 )
rs = node.right.data;
else
rs = 2 * (node.right.data);
if ((node.data == rs + ls))
return 1 ;
else
return 0 ;
}
return 0 ;
}
public static void main(String args[])
{
BinaryTree tree = new BinaryTree();
tree.root = new Node( 26 );
tree.root.left = new Node( 10 );
tree.root.right = new Node( 3 );
tree.root.left.left = new Node( 4 );
tree.root.left.right = new Node( 6 );
tree.root.right.right = new Node( 3 );
if (tree.isSumTree(tree.root) != 0 )
System.out.println( "The given tree is a SumTree" );
else
System.out.println( "The given tree is not a SumTree" );
}
}
|
C#
using System;
public class Node
{
public int data;
public Node left, right, nextRight;
public Node( int d)
{
data = d;
left = right = nextRight = null ;
}
}
public class BinaryTree{
public static Node root;
public int isLeaf(Node node)
{
if (node == null )
return 0;
if (node.left == null &&
node.right == null )
return 1;
return 0;
}
public int isSumTree(Node node)
{
int ls;
int rs;
if (node == null || isLeaf(node) == 1)
return 1;
if (isSumTree(node.left) != 0 &&
isSumTree(node.right) != 0)
{
if (node.left == null )
ls = 0;
else if (isLeaf(node.left) != 0)
ls = node.left.data;
else
ls = 2 * (node.left.data);
if (node.right == null )
rs = 0;
else if (isLeaf(node.right) != 0)
rs = node.right.data;
else
rs = 2 * (node.right.data);
if ((node.data == rs + ls))
return 1;
else
return 0;
}
return 0;
}
static public void Main()
{
BinaryTree tree = new BinaryTree();
BinaryTree.root = new Node(26);
BinaryTree.root.left = new Node(10);
BinaryTree.root.right = new Node(3);
BinaryTree.root.left.left = new Node(4);
BinaryTree.root.left.right = new Node(6);
BinaryTree.root.right.right = new Node(3);
if (tree.isSumTree(BinaryTree.root) != 0)
{
Console.WriteLine( "The given tree is a SumTree" );
}
else
{
Console.WriteLine( "The given tree is " +
"not a SumTree" );
}
}
}
|
Output:
The given tree is a SumTree
Time Complexity: O(n)
Please write comments if you find the above codes/algorithms incorrect, or find other ways to solve the same problem.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.