Maximum Level Sum of a Binary Tree
Given the root of a binary tree, the level of its root is 1, the level of its children is 2, and so on.
Return the smallest level x such that the sum of all the values of nodes at level x is maximal.
Example 1
1
/ \
7 0
/ \
7 -8Input
root = [1,7,0,7,-8,null,null]Output
2Level 1 sum is 1, level 2 sum is 7, and level 3 sum is -1, so level 2 has the maximum sum.
Example 2
989
\
10250
/ \
98693 -89388
\
-32127Input
root = [989,null,10250,98693,-89388,null,null,null,-32127]Output
2The level with the maximum sum is level 2.
Constraints
- The number of nodes in the tree is in the range
[1, 10^4]. -10^5 <= Node.val <= 10^5