Convert a Binary Tree such that every node stores the sum of all nodes in its right subtree
Given a binary tree, change the value in each node to sum of all the values in the nodes in the right subtree including its own.
Examples:
Input :
1
/ \
2 3
Output :
4
/ \
2 3
Input :
1
/ \
2 3
/ \ \
4 5 6
Output :
10
/ \
7 9
/ \ \
4 5 6
Approach : The idea is to traverse the given binary tree in bottom up manner. Recursively compute the sum of nodes in right and left subtrees. Accumulate sum of nodes in the right subtree to the current node and return sum of nodes under current subtree.
Below is the implementation of above approach.
C++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
Node *left, *right;
};
struct Node* createNode( int item)
{
Node* temp = new Node;
temp->data = item;
temp->left = NULL;
temp->right = NULL;
return temp;
}
int updateBTree(Node* root)
{
if (!root)
return 0;
if (root->left == NULL && root->right == NULL)
return root->data;
int rightsum = updateBTree(root->right);
int leftsum = updateBTree(root->left);
root->data += rightsum;
return root->data + leftsum;
}
void inorder( struct Node* node)
{
if (node == NULL)
return ;
inorder(node->left);
cout << node->data << " " ;
inorder(node->right);
}
int main()
{
struct Node* root = NULL;
root = createNode(1);
root->left = createNode(2);
root->right = createNode(3);
root->left->left = createNode(4);
root->left->right = createNode(5);
root->right->right = createNode(6);
updateBTree(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 Node createNode( int item)
{
Node temp = new Node();
temp.data = item;
temp.left = null ;
temp.right = null ;
return temp;
}
static int updateBTree(Node root)
{
if (root == null )
return 0 ;
if (root.left == null && root.right == null )
return root.data;
int rightsum = updateBTree(root.right);
int leftsum = updateBTree(root.left);
root.data += rightsum;
return root.data + leftsum;
}
static void inorder( Node node)
{
if (node == null )
return ;
inorder(node.left);
System.out.print( node.data + " " );
inorder(node.right);
}
public static void main(String args[])
{
Node root = null ;
root = createNode( 1 );
root.left = createNode( 2 );
root.right = createNode( 3 );
root.left.left = createNode( 4 );
root.left.right = createNode( 5 );
root.right.right = createNode( 6 );
updateBTree(root);
System.out.print( "Inorder traversal of the modified tree is \n" );
inorder(root);
}
}
|
Python3
class createNode:
def __init__( self , key):
self .data = key
self .left = None
self .right = None
def updateBTree( root):
if ( not root):
return 0
if (root.left = = None and
root.right = = None ):
return root.data
rightsum = updateBTree(root.right)
leftsum = updateBTree(root.left)
root.data + = rightsum
return root.data + leftsum
def inorder(node):
if (node = = None ):
return
inorder(node.left)
print (node.data, end = " " )
inorder(node.right)
if __name__ = = '__main__' :
root = None
root = createNode( 1 )
root.left = createNode( 2 )
root.right = createNode( 3 )
root.left.left = createNode( 4 )
root.left.right = createNode( 5 )
root.right.right = createNode( 6 )
updateBTree(root)
print ( "Inorder traversal of the" ,
"modified tree is" )
inorder(root)
|
C#
using System;
class GFG
{
public class Node
{
public int data;
public Node left, right;
};
static Node createNode( int item)
{
Node temp = new Node();
temp.data = item;
temp.left = null ;
temp.right = null ;
return temp;
}
static int updateBTree(Node root)
{
if (root == null )
return 0;
if (root.left == null && root.right == null )
return root.data;
int rightsum = updateBTree(root.right);
int leftsum = updateBTree(root.left);
root.data += rightsum;
return root.data + leftsum;
}
static void inorder( Node node)
{
if (node == null )
return ;
inorder(node.left);
Console.Write( node.data + " " );
inorder(node.right);
}
public static void Main(String[] args)
{
Node root = null ;
root = createNode(1);
root.left = createNode(2);
root.right = createNode(3);
root.left.left = createNode(4);
root.left.right = createNode(5);
root.right.right = createNode(6);
updateBTree(root);
Console.Write( "Inorder traversal of the modified tree is \n" );
inorder(root);
}
}
|
Javascript
<script>
class Node
{
constructor()
{
this .data = 0;
this .left = null ;
this .right = null ;
}
};
function createNode(item)
{
var temp = new Node();
temp.data = item;
temp.left = null ;
temp.right = null ;
return temp;
}
function updateBTree(root)
{
if (root == null )
return 0;
if (root.left == null && root.right == null )
return root.data;
var rightsum = updateBTree(root.right);
var leftsum = updateBTree(root.left);
root.data += rightsum;
return root.data + leftsum;
}
function inorder(node)
{
if (node == null )
return ;
inorder(node.left);
document.write( node.data + " " );
inorder(node.right);
}
var root = null ;
root = createNode(1);
root.left = createNode(2);
root.right = createNode(3);
root.left.left = createNode(4);
root.left.right = createNode(5);
root.right.right = createNode(6);
updateBTree(root);
document.write( "Inorder traversal of the modified tree is <br>" );
inorder(root);
</script>
|
Output
Inorder traversal of the modified tree is
4 7 5 10 9 6
Complexity Analysis:
- Time Complexity: O(n)
- Auxiliary space: O(n) for implicit call stack as using recursion
Last Updated :
22 Aug, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...