Given a Binary Tree, change the value in each node to sum of all the values in the nodes in the left subtree including its own.
Examples:
Input :
1
/ \
2 3
Output :
3
/ \
2 3
Input
1
/ \
2 3
/ \ \
4 5 6
Output:
12
/ \
6 3
/ \ \
4 5 6
We strongly recommend you to minimize your browser and try this yourself first.
The idea is to traverse the given tree in bottom up manner. For every node, recursively compute sum of nodes in left and right subtrees. Add sum of nodes in left subtree to current node and return sum of nodes under current subtree.
Below is the implementation of above idea.
C++
#include<bits/stdc++.h>
using namespace std;
class node
{
public :
int data;
node* left, *right;
node( int data)
{
this ->data = data;
this ->left = NULL;
this ->right = NULL;
}
};
int updatetree(node *root)
{
if (!root)
return 0;
if (root->left == NULL && root->right == NULL)
return root->data;
int leftsum = updatetree(root->left);
int rightsum = updatetree(root->right);
root->data += leftsum;
return root->data + rightsum;
}
void inorder(node* node)
{
if (node == NULL)
return ;
inorder(node->left);
cout<<node->data<< " " ;
inorder(node->right);
}
int main()
{
struct node *root = new node(1);
root->left = new node(2);
root->right = new node(3);
root->left->left = new node(4);
root->left->right = new node(5);
root->right->right = new node(6);
updatetree(root);
cout << "Inorder traversal of the modified tree is: \n" ;
inorder(root);
return 0;
}
|
C
#include<bits/stdc++.h>
using namespace std;
struct node
{
int data;
struct node* left, *right;
};
int updatetree(node *root)
{
if (!root)
return 0;
if (root->left == NULL && root->right == NULL)
return root->data;
int leftsum = updatetree(root->left);
int rightsum = updatetree(root->right);
root->data += leftsum;
return root->data + rightsum;
}
void inorder( struct node* node)
{
if (node == NULL)
return ;
inorder(node->left);
printf ( "%d " , node->data);
inorder(node->right);
}
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(1);
root->left = newNode(2);
root->right = newNode(3);
root->left->left = newNode(4);
root->left->right = newNode(5);
root->right->right = newNode(6);
updatetree(root);
cout << "Inorder traversal of the modified tree is \n" ;
inorder(root);
return 0;
}
|
Java
class GfG {
static class node
{
int data;
node left, right;
}
static int updatetree(node root)
{
if (root == null )
return 0 ;
if (root.left == null && root.right == null )
return root.data;
int leftsum = updatetree(root.left);
int rightsum = updatetree(root.right);
root.data += leftsum;
return root.data + rightsum;
}
static void inorder(node node)
{
if (node == null )
return ;
inorder(node.left);
System.out.print(node.data + " " );
inorder(node.right);
}
static node newNode( int data)
{
node node = new node();
node.data = data;
node.left = null ;
node.right = null ;
return (node);
}
public static void main(String[] args)
{
node root = newNode( 1 );
root.left = newNode( 2 );
root.right = newNode( 3 );
root.left.left = newNode( 4 );
root.left.right = newNode( 5 );
root.right.right = newNode( 6 );
updatetree(root);
System.out.println( "Inorder traversal of the modified tree is" );
inorder(root);
}
}
|
Python3
class newNode:
def __init__( self , key):
self .data = key
self .left = None
self .right = None
def updatetree(root):
if ( not root):
return 0
if (root.left = = None and
root.right = = None ) :
return root.data
leftsum = updatetree(root.left)
rightsum = updatetree(root.right)
root.data + = leftsum
return root.data + rightsum
def inorder(node) :
if (node = = None ) :
return
inorder(node.left)
print (node.data, end = " " )
inorder(node.right)
if __name__ = = '__main__' :
root = newNode( 1 )
root.left = newNode( 2 )
root.right = newNode( 3 )
root.left.left = newNode( 4 )
root.left.right = newNode( 5 )
root.right.right = newNode( 6 )
updatetree(root)
print ( "Inorder traversal of the modified tree is" )
inorder(root)
|
C#
using System;
class GfG
{
class node
{
public int data;
public node left, right;
}
static int updatetree(node root)
{
if (root == null )
return 0;
if (root.left == null && root.right == null )
return root.data;
int leftsum = updatetree(root.left);
int rightsum = updatetree(root.right);
root.data += leftsum;
return root.data + rightsum;
}
static void inorder(node node)
{
if (node == null )
return ;
inorder(node.left);
Console.Write(node.data + " " );
inorder(node.right);
}
static node newNode( int data)
{
node node = new node();
node.data = data;
node.left = null ;
node.right = null ;
return (node);
}
public static void Main()
{
node root = newNode(1);
root.left = newNode(2);
root.right = newNode(3);
root.left.left = newNode(4);
root.left.right = newNode(5);
root.right.right = newNode(6);
updatetree(root);
Console.WriteLine( "Inorder traversal of the modified tree is" );
inorder(root);
}
}
|
Javascript
<script>
class node
{
constructor()
{
this .data = 0;
this .left = null ;
this .right = null ;
}
}
function updatetree(root)
{
if (root == null )
return 0;
if (root.left == null && root.right == null )
return root.data;
var leftsum = updatetree(root.left);
var rightsum = updatetree(root.right);
root.data += leftsum;
return root.data + rightsum;
}
function inorder(node)
{
if (node == null )
return ;
inorder(node.left);
document.write(node.data + " " );
inorder(node.right);
}
function newNode(data)
{
var nod = new node();
nod.data = data;
nod.left = null ;
nod.right = null ;
return (nod);
}
var root = newNode(1);
root.left = newNode(2);
root.right = newNode(3);
root.left.left = newNode(4);
root.left.right = newNode(5);
root.right.right = newNode(6);
updatetree(root);
document.write( "Inorder traversal of the modified tree is<br>" );
inorder(root);
</script>
|
Output
Inorder traversal of the modified tree is:
4 6 5 12 3 6
Time Complexity: O(n)
Auxiliary space: O(n) for implicit call stack as using recursion
Thanks to Gaurav Ahrirwar for suggesting this solution.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!