#1161
Maximum Level Sum of a Binary Tree
pupil · 555 · lc medium +28 · verified · 70% accepted · 4,122 likes · top 78%
Description
You are given the root of a binary tree where the root is at level 1, its children are at level 2, and so on.
Find and return the smallest level number x whose nodes have the greatest total value sum.
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.
So we return the level with the maximum sum which is level 2.
Example 2:
Input: root = [989,null,10250,98693,-89388,null,null,null,-32127]
Output: 2
Code
1
2
3
4
5
6
7
8
9