Give an algorithm for finding the sum of all elements in a binary tree.
In the above binary tree sum = 106.
The idea is to recursively, call left subtree sum, right subtree sum and add their values to current node’s data.
Sum of all the elements is: 36
This article is contributed by Prakriti Gupta. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Construct XOR tree by Given leaf nodes of Perfect Binary Tree
- XOR of path between any two nodes in a Binary Tree
- Sink Odd nodes in Binary Tree
- Sum of all nodes at Kth level in a Binary Tree
- Sum of all the Boundary Nodes of a Binary Tree
- Sum of nodes in the right view of the given binary tree
- Sum of all leaf nodes of binary tree
- Sum of nodes in top view of binary tree
- Sink even nodes in Binary Tree
- Product of all nodes in a Binary Tree
- Check if two nodes in a Binary Tree are siblings
- Print nodes in the Top View of Binary Tree | Set 3
- Print all internal nodes of a Binary tree
- Count Non-Leaf nodes in a Binary Tree
- Print path between any two nodes in a Binary Tree
- Nodes at Kth level without duplicates in a Binary Tree
- Print all nodes between two given levels in Binary Tree
- Print nodes in top view of Binary Tree | Set 2
- Print Levels of all nodes in a Binary Tree
- Sum of nodes at maximum depth of a Binary Tree | Set 2