Find the level of a binary tree with the largest sum of node values.
Given the root of a binary tree, compute the sum of values at each depth level. Return the level number that has the maximum sum.
If multiple levels have the same maximum sum, return the smallest level index.
The root is considered level 1.
Example 1
Input
root = [1,7,0,7,-8,null,null]
Output
2
Explanation
Level 1 sum = 1. Level 2 sum = 7 + 0 = 7. Level 3 sum = 7 + (-8) = -1. The maximum sum is at level 2.
Example 2
Input
root = [1,-2,3,4,5,-6,2]
Output
3
Explanation
Level 1 sum = 1. Level 2 sum = 1. Level 3 sum = 5. The maximum sum is at level 3.
Premium problem context
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.