A binary tree of warehouse-bin item counts is given in level-order form (a single line of space-separated tokens, null marking a missing child; every non-null node contributes exactly two following tokens for its children).
For every depth level of the tree (the root is level 0), compute the sum of the values of all nodes at that level. Print the single largest such level-sum.
Line 1: space-separated level-order tokens describing the binary tree (integers and the token null).
A single integer: the maximum sum of node values found at any one level. If the tree is empty, print 0.
Example 1
Input
1 2 3
Expected
5
Explanation
Level 0 sum = 1. Level 1 sum = 2+3 = 5. The maximum is 5.
Example 2
Input
-10 20 20
Expected
40
Explanation
Level 0 sum = -10. Level 1 sum = 20+20 = 40. The maximum is 40.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →