# 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
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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++

 `// C++ program to store sum of nodes in ` `// right subtree in every node ` `#include ` `using` `namespace` `std; ` ` `  `// Node of tree ` `struct` `Node { ` `    ``int` `data; ` `    ``Node *left, *right; ` `}; ` ` `  `// Function to create a new node ` `struct` `Node* createNode(``int` `item) ` `{ ` `    ``Node* temp = ``new` `Node; ` `    ``temp->data = item; ` `    ``temp->left = NULL; ` `    ``temp->right = NULL; ` ` `  `    ``return` `temp; ` `} ` ` `  `// Function to build new tree with  ` `// all nodes having the sum of all  ` `// nodes in its right subtree ` `int` `updateBTree(Node* root) ` `{ ` `    ``// Base cases ` `    ``if` `(!root) ` `        ``return` `0; ` `    ``if` `(root->left == NULL && root->right == NULL) ` `        ``return` `root->data; ` ` `  `    ``// Update right and left subtrees ` `    ``int` `rightsum = updateBTree(root->right); ` `    ``int` `leftsum = updateBTree(root->left); ` ` `  `    ``// Add rightsum to current node ` `    ``root->data += rightsum; ` ` `  `    ``// Return sum of values under root ` `    ``return` `root->data + leftsum; ` `} ` ` `  `// Function to traverse tree in inorder way ` `void` `inorder(``struct` `Node* node) ` `{ ` `    ``if` `(node == NULL) ` `        ``return``; ` `    ``inorder(node->left); ` `    ``cout << node->data << ``" "``; ` `    ``inorder(node->right); ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``/* Let us construct a binary tree ` `            ``1 ` `           ``/ \ ` `          ``2   3 ` `         ``/ \   \ ` `        ``4   5   6       */` `    ``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); ` ` `  `    ``// new tree construction ` `    ``updateBTree(root); ` ` `  `    ``cout << ``"Inorder traversal of the modified tree is \n"``; ` `    ``inorder(root); ` ` `  `    ``return` `0; ` `} `

## Python3

# Program to convert expression tree
# from prefix expression

# Helper function that allocates a new
# node with the given data and None
# left and right poers.
class createNode:

# Conto create a new node
def __init__(self, key):
self.data = key
self.left = None
self.right = None

# Function to build new tree with
# all nodes having the sum of all
# nodes in its right subtree
def updateBTree( root):

# Base cases
if (not root):
return 0
if (root.left == None and
root.right == None):
return root.data

# Update right and left subtrees
rightsum = updateBTree(root.right)
leftsum = updateBTree(root.left)

# Add rightsum to current node
root.data += rightsum

# Return sum of values under root
return root.data + leftsum

# Function to traverse tree in inorder way
def inorder(node):

if (node == None):
return
inorder(node.left)
print(node.data, end = ” “)
inorder(node.right)

# Driver Code
if __name__ == ‘__main__’:

“”” Let us convert binary tree
1
/ \
2 3
/ \ \
4 5 6 “””
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)

# new tree construction
updateBTree(root)

print(“Inorder traversal of the”,
“modified tree is”)
inorder(root)

# This code is contributed by
# Shubham Singh(SHUBHAMSINGH10)

Output:

```Inorder traversal of the modified tree is
4 7 5 10 9 6
```

Time Complexity: O(n)

My Personal Notes arrow_drop_up

Article Tags :
Practice Tags :

1

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.