All problems
Tree Level Sums
Problem
Compute the sum of values at each level of a binary tree given in level order.
tree is a LeetCode-style list with None marking missing nodes; the children of index i are at 2*i + 1 and 2*i + 2. Traverse level by level and add the real values.
Return one sum per non-empty level ([] for an empty tree).
Hint
Collect child indices from non-None nodes only; stop when a level contributes nothing.
Test cases
case 1
in: [[1,2,3,4,null,6]]
expected: [1,5,10]
case 2
in: [[]]
expected: []
case 3
in: [[5]]
expected: [5]
case 4
in: [[1,null,3]]
expected: [1,3]
Starter codePython
def level_sums(tree):
# Your code here
pass4 test cases · run your code against them in the editor