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
10
/ \
-2 6
/ \ / \
8 -4 7 5
should be changed to
20(4-2+12+6)
/ \
4(8-4) 12(7+5)
/ \ / \
0 0 0 0
Solution:
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).
C++
#include <bits/stdc++.h>
using namespace std;
class node
{
public :
int data;
node *left;
node *right;
};
int toSumTree(node *Node)
{
if (Node == NULL)
return 0;
int old_val = Node->data;
Node->data = toSumTree(Node->left) + toSumTree(Node->right);
return Node->data + old_val;
}
void printInorder(node* Node)
{
if (Node == NULL)
return ;
printInorder(Node->left);
cout<< " " <<Node->data;
printInorder(Node->right);
}
node* newNode( int data)
{
node *temp = new node;
temp->data = data;
temp->left = NULL;
temp->right = NULL;
return temp;
}
int main()
{
node *root = NULL;
int x;
root = newNode(10);
root->left = newNode(-2);
root->right = newNode(6);
root->left->left = newNode(8);
root->left->right = newNode(-4);
root->right->left = newNode(7);
root->right->right = newNode(5);
toSumTree(root);
cout<< "Inorder Traversal of the resultant tree is: \n" ;
printInorder(root);
return 0;
}
|
C
#include<stdio.h>
struct node
{
int data;
struct node *left;
struct node *right;
};
int toSumTree( struct node *node)
{
if (node == NULL)
return 0;
int old_val = node->data;
node->data = toSumTree(node->left) + toSumTree(node->right);
return node->data + old_val;
}
void printInorder( struct node* node)
{
if (node == NULL)
return ;
printInorder(node->left);
printf ( "%d " , node->data);
printInorder(node->right);
}
struct node* newNode( int data)
{
struct node *temp = new struct node;
temp->data = data;
temp->left = NULL;
temp->right = NULL;
return temp;
}
int main()
{
struct node *root = NULL;
int x;
root = newNode(10);
root->left = newNode(-2);
root->right = newNode(6);
root->left->left = newNode(8);
root->left->right = newNode(-4);
root->right->left = newNode(7);
root->right->right = newNode(5);
toSumTree(root);
printf ( "Inorder Traversal of the resultant tree is: \n" );
printInorder(root);
getchar ();
return 0;
}
|
Java
class Node
{
int data;
Node left, right;
Node( int item)
{
data = item;
left = right = null ;
}
}
class BinaryTree
{
Node root;
int toSumTree(Node node)
{
if (node == null )
return 0 ;
int old_val = node.data;
node.data = toSumTree(node.left) + toSumTree(node.right);
return node.data + old_val;
}
void printInorder(Node node)
{
if (node == null )
return ;
printInorder(node.left);
System.out.print(node.data + " " );
printInorder(node.right);
}
public static void main(String args[])
{
BinaryTree tree = new BinaryTree();
tree.root = new Node( 10 );
tree.root.left = new Node(- 2 );
tree.root.right = new Node( 6 );
tree.root.left.left = new Node( 8 );
tree.root.left.right = new Node(- 4 );
tree.root.right.left = new Node( 7 );
tree.root.right.right = new Node( 5 );
tree.toSumTree(tree.root);
System.out.println( "Inorder Traversal of the resultant tree is:" );
tree.printInorder(tree.root);
}
}
|
Python3
class node:
def __init__( self , data):
self .left = None
self .right = None
self .data = data
def toSumTree(Node) :
if (Node = = None ) :
return 0
old_val = Node.data
Node.data = toSumTree(Node.left) + \
toSumTree(Node.right)
return Node.data + old_val
def printInorder(Node) :
if (Node = = None ) :
return
printInorder(Node.left)
print (Node.data, end = " " )
printInorder(Node.right)
def newNode(data) :
temp = node( 0 )
temp.data = data
temp.left = None
temp.right = None
return temp
if __name__ = = "__main__" :
root = None
x = 0
root = newNode( 10 )
root.left = newNode( - 2 )
root.right = newNode( 6 )
root.left.left = newNode( 8 )
root.left.right = newNode( - 4 )
root.right.left = newNode( 7 )
root.right.right = newNode( 5 )
toSumTree(root)
print ( "Inorder Traversal of the resultant tree is: " )
printInorder(root)
|
C#
using System;
public class Node
{
public int data;
public Node left, right;
public Node( int item)
{
data = item;
left = right = null ;
}
}
class GFG
{
public Node root;
public virtual int toSumTree(Node node)
{
if (node == null )
{
return 0;
}
int old_val = node.data;
node.data = toSumTree(node.left) +
toSumTree(node.right);
return node.data + old_val;
}
public virtual void printInorder(Node node)
{
if (node == null )
{
return ;
}
printInorder(node.left);
Console.Write(node.data + " " );
printInorder(node.right);
}
public static void Main( string [] args)
{
GFG tree = new GFG();
tree.root = new Node(10);
tree.root.left = new Node(-2);
tree.root.right = new Node(6);
tree.root.left.left = new Node(8);
tree.root.left.right = new Node(-4);
tree.root.right.left = new Node(7);
tree.root.right.right = new Node(5);
tree.toSumTree(tree.root);
Console.WriteLine( "Inorder Traversal of " +
"the resultant tree is:" );
tree.printInorder(tree.root);
}
}
|
Javascript
<script>
class Node
{
constructor(item)
{
this .data = item;
this .left = null ;
this .right = null ;
}
}
var root = null ;
function toSumTree(node)
{
if (node == null )
{
return 0;
}
var old_val = node.data;
node.data = toSumTree(node.left) +
toSumTree(node.right);
return node.data + old_val;
}
function printInorder(node)
{
if (node == null )
{
return ;
}
printInorder(node.left);
document.write(node.data + " " );
printInorder(node.right);
}
root = new Node(10);
root.left = new Node(-2);
root.right = new Node(6);
root.left.left = new Node(8);
root.left.right = new Node(-4);
root.right.left = new Node(7);
root.right.right = new Node(5);
toSumTree(root);
document.write( "Inorder Traversal of " +
"the resultant tree is:<br>" );
printInorder(root);
</script>
|
OutputInorder Traversal of the resultant tree is:
0 4 0 20 0 12 0
Time Complexity: The solution involves a simple traversal of the given tree. So the time complexity is O(n) where n is the number of nodes in the given Binary Tree.
Auxiliary Space : O(1) since using constant variables
Using postorder concept:
The idea is to use post order concept and store the value of left and right subtree and return it accordingly.
Steps to solve:
1. check if the root node is not equal to null:
- declare l , r variable to recurse left and right.
- declare temp variable to store to value of root data.
- update the root data to l+r.
- return temp+l+r.
2. Else return 0.
Implementation of the approach:
C++
#include <bits/stdc++.h>
using namespace std;
class node {
public :
int data;
node* left;
node* right;
};
int toSumTree(node* Node)
{
if (Node != NULL) {
int l = toSumTree(Node->left);
int r = toSumTree(Node->right);
int temp = Node->data;
Node->data = l + r;
return temp + l + r;
}
else
return 0;
}
void printInorder(node* Node)
{
if (Node == NULL)
return ;
printInorder(Node->left);
cout << " " << Node->data;
printInorder(Node->right);
}
node* newNode( int data)
{
node* temp = new node;
temp->data = data;
temp->left = NULL;
temp->right = NULL;
return temp;
}
int main()
{
node* root = NULL;
int x;
root = newNode(10);
root->left = newNode(-2);
root->right = newNode(6);
root->left->left = newNode(8);
root->left->right = newNode(-4);
root->right->left = newNode(7);
root->right->right = newNode(5);
toSumTree(root);
cout
<< "Inorder Traversal of the resultant tree is: \n" ;
printInorder(root);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class Node {
int data;
Node left, right;
Node( int item) {
data = item;
left = right = null ;
}
}
class GFG {
public static int toSumTree(Node root)
{
if (root != null ) {
int l = toSumTree(root.left);
int r = toSumTree(root.right);
int temp = root.data;
root.data = l + r;
return temp + l + r;
}
else
return 0 ;
}
public static void printInorder(Node root)
{
if (root == null )
return ;
printInorder(root.left);
System.out.println(root.data);
printInorder(root.right);
}
public static Node newNode( int data)
{
Node temp = new Node(data);
temp.data = data;
temp.left = null ;
temp.right = null ;
return temp;
}
public static void main (String[] args) {
Node root = null ;
int x;
root = newNode( 10 );
root.left = newNode(- 2 );
root.right = newNode( 6 );
root.left.left = newNode( 8 );
root.left.right = newNode(- 4 );
root.right.left = newNode( 7 );
root.right.right = newNode( 5 );
toSumTree(root);
System.out.println( "Inorder Traversal of the resultant tree is: \n" );
printInorder(root);
}
}
|
C#
using System;
using System.Collections.Generic;
class GFG
{
class node {
public int data;
public node left;
public node right;
public node( int data)
{
this .data=data;
this .left= null ;
this .right= null ;
}
}
static int toSumTree(node Node)
{
if (Node != null ) {
int l = toSumTree(Node.left);
int r = toSumTree(Node.right);
int temp = Node.data;
Node.data = l + r;
return temp + l + r;
}
else
return 0;
}
static void printInorder(node Node)
{
if (Node == null )
return ;
printInorder(Node.left);
Console.Write( " " + Node.data);
printInorder(Node.right);
}
static void Main( string [] args)
{
node root = null ;
int x;
root = new node(10);
root.left = new node(-2);
root.right = new node(6);root.left.left = new node(8);
root.left.right = new node(-4);
root.right.left = new node(7);
root.right.right = new node(5);
toSumTree(root);
Console.Write( "Inorder Traversal of the resultant tree is: \n" );
printInorder(root);
}
}
|
Javascript
class Node
{
constructor(data)
{
this .data = data;
this .left = null ;
this .right = null ;
}
};
function toSumTree( Node)
{
if (Node != null )
{
let l = toSumTree(Node.left);
let r = toSumTree(Node.right);
let temp = Node.data;
Node.data = l + r;
return temp + l + r;
}
else
return 0;
}
function printInorder(Node)
{
if (Node == null )
return ;
printInorder(Node.left);
console.log( " " + Node.data);
printInorder(Node.right);
}
let x;
let root = new Node(10);
root.left = new Node(-2);
root.right = new Node(6);
root.left.left = new Node(8);
root.left.right = new Node(-4);
root.right.left = new Node(7);
root.right.right = new Node(5);
toSumTree(root);
console.log( "Inorder Traversal of the resultant tree is: <br>" );
printInorder(root);
|
Python3
class node:
def __init__( self , data):
self .left = None
self .right = None
self .data = data
def toSumTree(Node) :
if (Node = = None ) :
return 0
old_val = Node.data
Node.data = toSumTree(Node.left) + \
toSumTree(Node.right)
return Node.data + old_val
def printInorder(Node) :
if (Node = = None ) :
return
printInorder(Node.left)
print (Node.data, end = " " )
printInorder(Node.right)
def newNode(data) :
temp = node( 0 )
temp.data = data
temp.left = None
temp.right = None
return temp
if __name__ = = "__main__" :
root = None
x = 0
root = newNode( 10 )
root.left = newNode( - 2 )
root.right = newNode( 6 )
root.left.left = newNode( 8 )
root.left.right = newNode( - 4 )
root.right.left = newNode( 7 )
root.right.right = newNode( 5 )
toSumTree(root)
print ( "Inorder Traversal of the resultant tree is: " )
printInorder(root)
|
OutputInorder Traversal of the resultant tree is:
0 4 0 20 0 12 0
Time Complexity: O(n), where n is the number of nodes.
Auxiliary Space : O(1) since using constant variables
This approach is contributed by Prateek Kumar Singh (pkrsingh025).