A trellised orchard is arranged as a binary tree; each node is a fruit with an integer weight. The depth of the root is 0 and each child is one deeper than its parent. Consider the deepest level present in the tree (the maximum depth). Report the sum of the weights of all fruits at that deepest level.
Line 1: an integer k, the number of tokens on line 2.
Line 2: k space-separated tokens giving the tree in level order. The first token is the root; each subsequent token is an integer weight or null for a missing child. Children of null nodes are omitted.
A single integer: the total weight of all fruits at the deepest level.
Example 1
Input
7 1 2 3 4 5 6 7
Expected
22
Explanation
Depths: root at 0; then 2,3 at depth 1; then 4,5,6,7 at depth 2. The deepest level is depth 2, and 4 + 5 + 6 + 7 = 22.
Example 2
Input
7 1 2 3 null null 4 5
Expected
9
Explanation
Node 2 has no children, so the deepest level (depth 2) holds only 4 and 5 (the children of 3). Their sum is 9.
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 →