Given a root node to a tree, find the sum of all the leaf nodes which are at maximum depth from root node.
1 / \ 2 3 / \ / \ 4 5 6 7 Input : root(of above tree) Output : 22 Explanation: Nodes at maximum depth are 4, 5, 6, 7. So, the sum of these nodes = 22
Approach: There exists a recursive approach to this problem. This can also be solved using level order traversal and map. The idea is to do a traversal using a queue and keep track of current level. A map has been used to store the sum of nodes at the current level. Once all nodes are visited and the traversal is done, the last element of the map will contain the sum at the maximum depth of the tree.
Below is the implementation of the above approach:
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Print All Leaf Nodes of a Binary Tree from left to right | Set-2 ( Iterative Approach )
- Sum of nodes at maximum depth of a Binary Tree
- Sum of nodes at maximum depth of a Binary Tree | Set 2
- Check whether a binary tree is a full binary tree or not | Iterative Approach
- Check for Symmetric Binary Tree (Iterative Approach)
- Get level of a node in binary tree | iterative approach
- Iterative approach to check if a Binary Tree is Perfect
- Largest value in each level of Binary Tree | Set-2 (Iterative Approach)
- Deepest right leaf node in a binary tree | Iterative approach
- Iterative approach to check for children sum property in a Binary Tree
- Deepest left leaf node in a binary tree | iterative approach
- Construct Binary Tree from given Parent Array representation | Iterative Approach
- Count full nodes in a Binary tree (Iterative and Recursive)
- Iterative program to count leaf nodes in a Binary Tree
- Count half nodes in a Binary tree (Iterative and Recursive)
- Print all nodes at distance K from given node: Iterative Approach
- Find maximum among all right nodes in Binary Tree
- Maximum sum of nodes in Binary tree such that no two are adjacent
- Maximum sum of leaf nodes among all levels of the given binary tree
- Maximum sum of non-leaf nodes among all levels of the given binary tree
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : SHUBHAMSINGH10