Given a Binary Tree where each node has positive and negative values. Convert this to a tree where each node contains the sum of the left and right sub trees in the original tree. The values of leaf nodes are changed to 0.
For example, the following tree
8 -4 7 5
should be changed to
0 0 0 0
Do a traversal of the given tree. In the traversal, store the old value of the current node, recursively call for left and right subtrees and change the value of current node as sum of the values returned by the recursive calls. Finally return the sum of new value and value (which is sum of values in the subtree rooted with this node).
/* A tree node structure */
// 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
// Base case
if(node == NULL)
// Store the old value
intold_val = node->data;
// Recursively call for left and right subtrees and store the sum as