Given a Binary Tree having positive and negative nodes, the task is to find the maximum sum of leaf nodes among all level of the given binary tree.
Input: 4 / \ 2 -5 / \ -1 3 Output: 2 Sum of all leaves at 0th level is 0. Sum of all leaves at 1st level is -5. Sum of all leaves at 2nd level is 2. Hence maximum sum is 2. Input: 1 / \ 2 3 / \ \ 4 5 8 / \ 6 7 Output: 13
Approach: The idea to solve the above problem is to do level order traversal of tree. While doing traversal, process nodes of different level separately. For every level being processed, compute the sum of leaf nodes in the level and keep track of the maximum sum.
Below is the implementation of the above approach:
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.
- Maximum sum of non-leaf nodes among all levels of the given binary tree
- Print all nodes between two given levels in Binary Tree
- Print Levels of all nodes in a Binary Tree
- Construct XOR tree by Given leaf nodes of Perfect Binary Tree
- Sum of all leaf nodes of binary tree
- Print odd positioned nodes of even levels in level order of the given binary tree
- Print even positioned nodes of even levels in level order of the given binary tree
- Print odd positioned nodes of odd levels in level order of the given binary tree
- Print even positioned nodes of odd levels in level order of the given binary tree
- Count Non-Leaf nodes in a Binary Tree
- Product of all leaf nodes of binary tree
- Program to count leaf nodes in a binary tree
- Print all leaf nodes of a Binary Tree from left to right
- Print Sum and Product of all Non-Leaf nodes in Binary Tree
- Pairwise Swap leaf nodes in a binary tree
- Print the nodes of binary tree as they become the leaf node
- Print all leaf nodes of a binary tree from right to left
- Iterative program to count leaf nodes in a Binary Tree
- Print left and right leaf nodes separately in Binary Tree
- Generate Complete Binary Tree in such a way that sum of non-leaf nodes is minimum
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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.