Given a Binary Tree having positive and negative nodes, the task is to find maximum sum level in it.
Input : 4 / \ 2 -5 / \ /\ -1 3 -2 6 Output: 6 Explanation : Sum of all nodes of 0'th level is 4 Sum of all nodes of 1'th level is -3 Sum of all nodes of 0'th level is 6 Hence maximum sum is 6 Input : 1 / \ 2 3 / \ \ 4 5 8 / \ 6 7 Output : 17
This problem is a variation of maximum width problem. The idea is to do level order traversal of tree. While doing traversal, process nodes of different level separately. For every level being processed, compute sum of nodes in the level and keep track of maximum sum.
Maximum level sum is 17
Time Complexity : O(n)
Auxiliary Space : O(n)
This article is contributed by Shashank Mishra ( Gullu ). 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.
- Check if max sum level of Binary tree divides tree into two equal sum halves
- Difference between sums of odd level and even level nodes of a Binary Tree
- Count nodes from all lower levels smaller than minimum valued node of current level for every level in a Binary Tree
- Print nodes of a Binary Search Tree in Top Level Order and Reversed Bottom Level Order alternately
- Find Maximum Level Sum in Binary Tree using Recursion
- Maximum absolute difference between any two level sum in a Binary Tree
- Find maximum level product in Binary Tree
- Find the maximum node at a given level in a binary tree
- Queries to find the maximum Xor value between X and the nodes of a given level of a perfect binary tree
- Maximum sub-tree sum in a Binary Tree such that the sub-tree is also a BST
- Difference between sums of odd level and even level nodes in an N-ary Tree
- Given level order traversal of a Binary Tree, check if the Tree is a Min-Heap
- Complexity of different operations in Binary tree, Binary Search Tree and AVL tree
- Sum of all nodes at Kth level in a Binary Tree
- Maximum absolute difference between any two level sum in a N-ary Tree
- Maximum level sum in N-ary Tree
- Find if given vertical level of binary tree is sorted or not
- Find the numbers present at Kth level of a Fibonacci Binary Tree
- Find the Level of a Binary Tree with Width K
- Connect Nodes at same Level (Level Order Traversal)