Problem 406
Maximum Level Sum
Medium · 20 pts · Tree, Breadth-First Search, Binary Tree
Problem
The root of a binary tree is on level 1, its children on level 2, and so on. Find the level whose node values add up to the largest sum, and print that level's number. If several levels share the largest sum, print the smallest such level number. Values can be negative, so a deeper level does not always have a larger sum.
Input
The first line contains N, the number of tokens. The second line contains N tokens describing the tree in level order (breadth-first, left to right), where 'null' marks a missing child, as on LeetCode.
Output
Print the level number with the maximum sum.
Constraints
1 <= number of nodes <= 10000; -100000 <= value <= 100000
Example input
5 1 7 0 7 -8
Example output
2
Explanation
In the tree 1 / 7 0 / 7 -8, the level sums are 1, 7 + 0 = 7 and 7 - 8 = -1. Level 2 has the largest sum.