- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Program to find a tree by updating values with left and right subtree sum with itself in Python

Suppose we have a binary tree, we have to find the same tree but every node's value is replaced by its its value + all of the sums of its left and right subtrees.

So, if the input is like

then the output will be

To solve this, we will follow these steps −

Define a function tree_sum() . This will take root of a tree

if root is null, then

return 0

data of root := tree_sum(left of root) + tree_sum(right of root) + data of root

return data of root

From the main method, do the following:

tree_sum(root)

return root

Let us see the following implementation to get better understanding −

## Example

class TreeNode: def __init__(self, data, left = None, right = None): self.data = data self.left = left self.right = right def inorder(root): if root: inorder(root.left) print(root.data, end = ', ') inorder(root.right) class Solution: def solve(self, root): def tree_sum(root: TreeNode): if root is None: return 0 root.data = tree_sum(root.left) + tree_sum(root.right) + root.data return root.data tree_sum(root) return root ob = Solution() root = TreeNode(2) root.left = TreeNode(3) root.right = TreeNode(4) root.left.left = TreeNode(9) root.left.right = TreeNode(7) ob.solve(root) inorder(root)

## Input

root = TreeNode(12) root.left = TreeNode(8) root.right = TreeNode(15) root.left.left = TreeNode(3) root.left.right = TreeNode(10)

## Output

9, 19, 7, 25, 4,

- Related Articles
- Program to find most frequent subtree sum of a binary tree in Python
- Find largest subtree having identical left and right subtrees in Python
- Find largest subtree sum in a tree in C++
- Program to find largest binary search subtree from a given tree in Python
- Python Program to Print only Nodes in Left SubTree
- Program to delete all leaves with even values from a binary tree in Python
- Program to find sum of the right leaves of a binary tree in C++
- Find the largest Perfect Subtree in a given Binary Tree in Python
- Find the largest Complete Subtree in a given Binary Tree in Python
- Program to find sum of all numbers formed by path of a binary tree in python
- Program to find sum of all elements of a tree in Python
- Python Program to Find the Sum of all Nodes in a Tree
- Program to find sum of contiguous sublist with maximum sum in Python
- Updating a MySQL table with values from another table?
- Program to find index whose left and right elements sums are equal in Python

Advertisements