872. Find Maximum Level Sum in Binary Tree

EasyTreesBinary TreeBFS

Given the root of a binary tree, return the 1-indexed level (the root is level 1) whose node values have the greatest sum. If multiple levels tie, return the smallest such level number. An empty tree returns 0. The tree is given as a level-order array.

Input: A level-order array of the tree.

Output: Integer — the level with the maximum sum.

Examples

Example 1
Input: [1,7,0,7,-8]
Output: 2
Explanation: Level 2 sums to 7, the maximum.
Example 2
Input: [1]
Output: 1
Explanation: Only one level.
Example 3
Input: [989,null,10250,98693,-89388,null,null,null,-32127]
Output: 2
Explanation: Level 2 has the larger sum.

Constraints

Asked by

TCSInfosysWiproCognizantCapgemini
Solve this problem in the editor →