Given a Binary Tree, the task is to check if the maximum sum level divides the binary tree into the two parts of two equal sum halves.
Input: 1 / \ 2 3 / \ \ 4 5 8 / \ 2 4 Output: YES Explanation: The maximum sum level is 2 and its sum is (4 + 5 + 8 = 17) Sum of the upper half (1 + 2 + 3) = 6 Sum of the Lower half (2 + 4) = 6 Input: 10 / \ 20 30 / \ \ 4 5 1 Output: YES Explanation: The maximum sum level is 1 and its sum is (20 + 30 = 50) Sum of the upper half (10) = 10 Sum of the lower half (5 + 4 + 1) = 10
Approach: The idea is to use level order traversal to compute the sum of every level of the binary tree. Then, find the maximum sum of the in all the levels. Finally check that the total sum of all the levels less than the maximum level sum is equal to the total sum of the levels of the greater than the maximum level 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.
- Number of ways to divide a Binary tree into two halves
- Maximum cost of splitting given Binary Tree into two halves
- Check if removing an edge can divide a Binary Tree in two 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
- Given level order traversal of a Binary Tree, check if the Tree is a Min-Heap
- Difference between sums of odd level and even level nodes in an N-ary Tree
- Maximum absolute difference between any two level sum in a Binary Tree
- Min-Max Product Tree of a given Binary Tree
- Print nodes between two given level numbers of a binary tree
- Count nodes with two children at level L in a Binary Tree
- Check if the given array can represent Level Order Traversal of Binary Search Tree
- Check if a binary tree is sorted level-wise or not
- Check if value exists in level-order sorted complete binary tree
- Check if the level order traversal of a Binary Tree results in a palindrome
- Connect Nodes at same Level (Level Order Traversal)
- Check if the given binary tree has a sub-tree with equal no of 1's and 0's | Set 2
- Check if a Tree can be split into K equal connected components
- Find maximum level sum in 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 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.