Skip to content
DeepForgeby svx
All problems
EasyData Structuresds-098

Sum of Left Leaves

Problem

Sum the values of all left leaves in a binary tree given in level order.

A left leaf is a node that is the left child of its parent and has no children of its own. tree is a LeetCode-style list with None marking missing nodes.

Return the sum, or 0 when there are no left leaves.

Hint

For each left child, check that it has no children of its own.

Test cases

case 1
in: [[3,9,20,null,null,15,7]]
expected: 24
case 2
in: [[1,2,3,4,5]]
expected: 4
case 3
in: [[]]
expected: 0
case 4
in: [[1]]
expected: 0
case 5
in: [[1,2,null,3]]
expected: 3
Starter codePython
def sum_left_leaves(tree):
    # Your code here
    pass

5 test cases · run your code against them in the editor