Given a Binary Tree having positive and negative nodes, the task is to find the maximum sum level in it and print the maximum sum.
Input: 4 / \ 2 -5 / \ / \ -1 3 -2 6 Output: 6 Sum of all nodes of the 1st level is 4. Sum of all nodes of the 2nd level is -3. Sum of all nodes of the 3rd level is 6. Hence, the maximum sum is 6. Input: 1 / \ 2 3 / \ \ 4 5 8 / \ 6 7 Output: 17
Approach: Find the maximum level in the given binary tree then create an array sum where sum[i] will store the sum of the elements at level i.
Now, write a recursive function that takes a node of the tree and its level as the argument and updates the sum for the current level then makes recursive calls for the children with the updated level as one more than the current level (this is because children are at a level one more than their parent). Finally, print the maximum value from the sum array.
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.
- Difference between sums of odd level and even level nodes of a Binary Tree
- Print nodes of a Binary Search Tree in Top Level Order and Reversed Bottom Level Order alternately
- Print the nodes corresponding to the level value for each level 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
- Check if max sum level of Binary tree divides tree into two equal sum halves
- Find the node with maximum value in a Binary Search Tree using recursion
- Find maximum and minimum element in binary tree without using recursion or stack or queue
- Product of nodes at k-th level in a tree represented as string using Recursion
- Construct a Binary in Level Order using Recursion
- Find maximum level sum in Binary Tree
- Maximum absolute difference between any two level sum in a Binary Tree
- Find maximum level product in Binary Tree
- Queries to find the maximum Xor value between X and the nodes of a given level of a perfect binary tree
- Find the maximum node at a given level in a binary tree
- Difference between sums of odd level and even level nodes in an N-ary Tree
- Find the node with minimum value in a Binary Search Tree using recursion
- Maximum sub-tree sum in a Binary Tree such that the sub-tree is also a BST
- Given level order traversal of a Binary Tree, check if the Tree is a Min-Heap
- Leaf nodes from Preorder of a Binary Search Tree (Using Recursion)
- Zig-Zag traversal of a Binary Tree using Recursion
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.