dsa · easy

Sum of Left Leaves

A **leaf** has no children. A **left leaf** is a leaf that is the left child of its parent. The root is never a left leaf (it has no parent).

Arguments

Return the sum of every left-leaf value in root. Each node is [val, left, right] or None. An empty tree sums to 0.

Example

root = [3, [9, None, None], [20, [15, None, None], [7, None, None]]].

9 is a left child and a leaf → count it. 15 is a left child of 20 and a leaf → count it. 7 is a right child, so skip it. Sum 9 + 15 = 24.

[1, None, [2, None, None]] has a right leaf only → 0.

Constraints

Number of nodes in [0, 1000] -1000 <= val <= 1000 Hidden tests include near-max size for this bound; a slower-than-intended solution TLEs.

Examples

Example 1

Input:
[3, [9, None, None], [20, [15, None, None], [7, None, None]]]

Expected:
24

Example 2

Input:
[1, None, [2, None, None]]

Expected:
0

Open in the Dojo editor