Children Sum Parent | Practice | GeeksforGeeks

Given a Binary Tree. Check whether all of its nodes have the value equal to the sum of their child nodes.

Example 1:
Input:
     10
    /
  10 
Output: 1
Explanation: Here, every node is sum of
its left and right child.
Example 2:
Input:
       1
     /   \
    4     3
   /  \
  5    N
Output: 0
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 conditions.

Your Task:
You don't need to read input or print anything. Your task is to complete the function isSumProperty() that takes the root Node of the Binary Tree as input and returns 1 if all the nodes in the tree satisfy the following properties. Else, it returns 0.
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 child.  Also, leaves are considered to follow the property.

Expected Time Complexiy: O(N).
Expected Auxiliary Space: O(Height of the Tree).
 
Constraints:
1 <= N <= 105
1 <= Data on nodes <= 105
0
nitishkumarkushwaha18133 weeks ago
time : 0.3 sec
 
public static int isSumProperty(Node root)   {       // add your code here       if(root == null)           return 1;                  if(root.left == null && root.right == null)           return 1;                  int rightSum = 0;       int leftSum = 0;              if(root.left != null)           leftSum = root.left.data;                  if(root.right != null)           rightSum = root.right.data;                  if(root.data == (rightSum + leftSum) && (isSumProperty(root.left) == 1 && isSumProperty(root.right) == 1 ))           return 1;              else           return 0;          }
0
adityagagtiwari3 weeks ago
Java solution
Time Complexiy: O(N).Auxiliary Space: O(Height of the Tree)
class Tree
{
   //Function to check whether all nodes of a tree have the value 
   //equal to the sum of their child nodes.
   public static int isSumProperty(Node root)
   {
       // add your code here
        boolean flag =sumProperty(root);
        if(flag)
        {
            return 1;
        }
        return 0;
   }
   public static boolean sumProperty(Node root)
   {
       if(root==null)
       return true;
   
       if(root.left!=null && root.right!=null)
       {
           if(root.data==root.left.data+root.right.data)
           return sumProperty(root.left)&&sumProperty(root.right);
           else
           return false;
       }
       else if(root.left!=null)
       {
           if(root.data==root.left.data)
           return sumProperty(root.left);
           else
           return false;
       }
       else if(root.right!=null)
       {
           if(root.data==root.right.data)
           return sumProperty(root.right);
           else
           return false;
       }
       else if(root.left==null && root.right==null)
       {
           return true;
       }
       return false;
   }
}
0
1ashishchauhan20023 weeks ago
 if(root==NULL){       return 1;   }   if(root->left==NULL && root->right==NULL){       return 1;   }  int sum=0;  if(root->left){      sum+=root->left->data;  }  if(root->right){      sum+=root->right->data;  }  if(sum==root->data){  return isSumProperty(root->right) && isSumProperty(root->left);  }  return 0;
+1
amishasahu3283 weeks ago
int isSumProperty(Node *root)
    {
     // Add your code here
         if(!root) return 1;
         if(!root->left && !root->right) return 1;
         int sum = 0;
         if(root->left)
            sum += root->left->data;
         if(root->right)
            sum += root->right->data;
            
        return (sum == root->data && isSumProperty(root->left) && isSumProperty(root->right) );
    }
+1
adarshgupta4014 weeks ago
6 lines Java Soln
 
public static int isSumProperty(Node root)
    {
        if(root == null ||(root.left==null&&root.right==null)) return 1;
        int childSum = 0;
        if(root.left!= null) childSum += root.left.data;
        if(root.right!= null) childSum += root.right.data;
        
        return (childSum==root.data && 
                isSumProperty(root.left)==1 && 
                isSumProperty(root.right)==1 
                )?1:0;
    }
0
mayank20211 month ago
C++ : 0.0/1.0int isSumProperty(Node *root)   {    int left=0, right=0;        if(root && (root->left || root->right ))     {      if (root->left) left=root->left->data;      if (root->right) right=root->right->data;            if(root->data == left+right)       {       left=isSumProperty(root->left);       right=isSumProperty(root->right);            if(left && right) return 1;      else return 0;                  }       else return 0;                }     return 1;           }
+1
crawler1 month ago
int isSumProperty(Node *root)
    {
        if(!root)
            return 1;
        if(!root->left and !root->right)
            return 1;
        int val = (root->left ? root->left->data : 0);
        val += (root->right ? root->right->data : 0);
        return val == root->data and isSumProperty(root->left) and isSumProperty(root->right);
    }
0
detroix071 month ago
int helper(Node* root, int &f) {     // base case      if(!root) return 0;          if(!root->left && !root->right) {       return root->data;     }          int left = helper(root->left,f);     int right = helper(root->right,f);          if(left+right!=root->data) f=0;        }
   int isSumProperty(Node *root)   {      int f = 1;      int ans = helper(root,f);      return f;   }
+1
harshupadhayay9061 month ago
    int isSumProperty(Node *root)   {       if(!root || (!root->right && !root->left))           return 1;       int sum = 0;       if(root->right) sum += root->right->data;       if(root->left)  sum += root->left->data;       if(sum == root->data)           return isSumProperty(root->right) && isSumProperty(root->left);       return false;    // Add your code here   }
0
tharabhai1 month ago
C++ recursive solution:
 
int isSumProperty(Node *root)   {    // Add your code here       if(!root)           return 1;       if(isSumProperty(root->left)==0 || isSumProperty(root->right)==0)           return 0;       if(root->left!=NULL && root->right!=NULL)       {           if(root->data==(root->left->data+root->right->data))               return 1;       }       else if(root->left!=NULL)       {           if(root->data==root->left->data)               return 1;       }       else if(root->right!=NULL)       {           if(root->data==root->right->data)               return 1;       }       else           return 1;       return 0;   }
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.