Given a Binary Tree having positive and negative nodes, the task is to find the maximum sum of non-leaf nodes among all level of given binary tree.
Input: 4 / \ 2 -5 / \ -1 3 Output: 4 Sum of all non-leaf nodes at 0th level is 4. Sum of all non-leaf nodes at 1st level is 2. Sum of all non-leaf nodes at 2nd level is 0. Hence maximum sum is 4 Input: 1 / \ 2 3 / \ \ 4 5 8 / \ 6 7 Output: 8
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 non-leaf nodes in the level and keep track of the maximum sum.
Below is the implementation of the above approach:
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- Maximum sum of leaf nodes among all levels of the given binary tree
- Print Levels of all nodes in a Binary Tree
- Print all nodes between two given levels in Binary Tree
- Print odd positioned nodes of odd 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 even levels in level order of the given binary tree
- Print even positioned nodes of odd levels in level order of the given binary tree
- Maximum sum of nodes in Binary tree such that no two are adjacent
- Find maximum among all right nodes in Binary Tree
- Sum of nodes at maximum depth of a Binary Tree | Set 2
- Sum of nodes at maximum depth of a Binary Tree
- Maximum sum of nodes in Binary tree such that no two are adjacent | Dynamic Programming
- Sum of nodes at maximum depth of a Binary Tree | Iterative Approach
- Print the nodes at odd levels of a tree
- Maximum length cycle that can be formed by joining two nodes of a 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 : rituraj_jain