Transform to Sum Tree | Practice | GeeksforGeeks

Given a Binary Tree of size N , where each node can have positive or negative values. Convert this to a tree where each node contains the sum of the left and right sub trees of the original tree. The values of leaf nodes are changed to 0.
Example 1:
Input:
             10
          /      \
        -2        6
       /   \     /  \
     8     -4   7    5

Output:
            20
          /    \
        4        12
       /  \     /  \
     0     0   0    0

Explanation:

           (4-2+12+6)
          /           \
      (8-4)          (7+5)
       /   \         /  \
      0     0       0    0
 
Your Task:  
You dont need to read input or print anything. Complete the function toSumTree() which takes root node as input parameter and modifies the given tree in-place.
Note: If you click on Compile and Test the output will be the in-order traversal of the modified tree.

Expected Time Complexity: O(N)
Expected Auxiliary Space: O(height of tree)
 
Constraints:
1 ≤ N ≤ 104
+1
goelyash3 weeks ago
    int solve(Node* root){
        if(!root)return 0;
        int x =root->data;
        int l=solve(root->left);
        int r = solve(root->right);
        root->data = l+r;
        return l+r+x;
    }
    void toSumTree(Node *node)
    {
        int x=solve(node);
    }
0
keshavkjha19993 weeks ago
Nice and Easy C++
int sumTree(Node *node){
        int sumLeft, sumRight;
        if(!node->left&&!node->right){
            sumLeft = 0;
            sumRight = 0;
        } else if(!node->right){
            sumLeft = node->left->data + sumTree(node->left);
            sumRight = 0;
        } else if(!node->left){
            sumLeft = 0;
            sumRight = node->right->data + sumTree(node->right);
        } else {
            sumLeft = node->left->data + sumTree(node->left);
            sumRight = node->right->data + sumTree(node->right);
        }
        return sumLeft + sumRight;
    }
    
    void toSumTree(Node *node){
        // Your code here
        queue<Node *> q;
        q.push(node);
        int sum;
        while(!q.empty()){
            Node* currentNode = q.front();
            q.pop();
            sum = sumTree(currentNode);
            currentNode->data = sum;
            if(currentNode->left){
                q.push(currentNode->left);
            }
            if(currentNode->right){
                q.push(currentNode->right);
            }
        }
    }
+1
jaydhurat1 month ago
class Solution{   public void toSumTree(Node root){        sum(root);   }   public int  sum(Node root)   {       if(root==null)       {           return 0;       }       int x=sum(root.left);       int y=sum(root.right);       int data=root.data;       root.data=x+y;       return x+y+data;   }}
 
0
badgujarsachin831 month ago
int cheak(Node* node){
        if(node==NULL){
            return 0;
        }
        int a=cheak(node->left);
        int b=cheak(node->right);
        int x=node->data;
        node->data=a+b;
        return a+b+x;
    }
    // Convert a given tree to a tree where every node contains sum of values of
    // nodes in left and right subtrees in the original tree
    void toSumTree(Node *node)
    {
        // Your code here
        cheak(node);
    }
0
sfazal0002 months ago
int solve(node* root){   // base case   if(root == NULL)   {       return 0;   }      // hypothesis   int l= solve(root->left);   int r = solve(root->right);     // induction   int x = root->data;       root->data = l+r;      return l+r+x;}
 
void binaryTreeToSumTree(node* root){      solve(root);   }
0
tejasbhagat542 months ago
Simple Java Solution :
class Solution{   public int solve(Node root)   {       if(root==null)return 0;       int a=solve(root.left);       int b=solve(root.right);       int x=root.data;       root.data=a+b;       return a+b+x;   }   public void toSumTree(Node root)   {       solve(root);   }}
0
vrajeshmodi992 months ago
int helper(Node*root)
    {
        if(!root)return 0;
        
        int nl = helper(root->left);
        int nr = helper(root->right);
        
        int sum = root->data;
        root->data = nl+nr;
        
        return (root->data+sum);
    }
    
    void toSumTree(Node *node)
    {
        helper(node);
    }
0
ritikshrivastava6645
This comment was deleted.
+1
wolfofsv2 months ago
Simple traversal 
  int sum_util(Node* node){
        if(node){
            int child = sum_util(node -> left) + sum_util(node -> right);
            int store = node -> data;
            node -> data = child;
            return child + store;
        }
        return 0;
    }
    
    void toSumTree(Node *node)
    {
        // Your code here
        sum_util(node);
    }
};
0
hamidnourashraf2 months ago
class Solution:
    def rec_sum(self, root):
        if root is None:
            return 0
        elif root.left is None and root.right is None:
            val = root.data
            root.data = 0
            return val
        else:
            val1 = self.rec_sum(root.left) 
            val2 = self.rec_sum(root.right)
            val = val1+val2+root.data
            root.data = val1+val2
            return val
    def toSumTree(self, root) :
        self.rec_sum(root)
        return root
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.