We’re preparing your current view and syncing the latest data.
Maximum Level Sum in Binary Tree
gfgGiven the root of a binary tree, return the level (1-indexed) of the tree that has the maximum sum of its node values. If there is a tie, return the smallest level number with the maximum sum.
The input is the root of a binary tree.
Return an integer representing the level with the maximum sum of node values.
The number of nodes in the tree is in the range [1, 10^4]. Node values are in the range [-10^5, 10^5].
Example 1
Input
root = [1,7,0,7,-8,null,null]
Output
2
Explanation
The sum at level 1 is 1, level 2 is 7 + 0 = 7, and level 3 is 7 + (-8) = -1. The maximum sum is 7 at level 2.