Skills101
← All coding problems

Sum of Left Leaves

easyTrees & BST II+10 XPTime limit 2s

A left leaf is a leaf (a node with no children) that is the left child of its parent. Find the sum of all left leaves in the tree.

Tree format. A binary tree is given on one line in level order (top to bottom, left to right), with null for a missing child — the same way most interview platforms show trees. Trailing nulls are left out, and an empty tree is the single word null. Example: 3 9 20 null null 15 7 is

    3
   / \
  9   20
     /  \
    15   7

Input: one tree.

Output: the sum (0 if there are no left leaves). A tree with only a root has no left leaves — the root is nobody's child.

Constraints: 0 ≤ nodes ≤ 10⁴, −1000 ≤ value ≤ 1000. The tree has at most 10⁴ nodes and its height is at most 500, so recursion is safe in every language.

Think about it: a node cannot tell by itself whether it is a left child. Check from the parent: if node.left exists and has no children, add its value.

Sample input 1

3 9 20 null null 15 7

Sample output 1

24

Sample input 2

1

Sample output 2

0

Read input from standard input and print the answer to standard output. Your code runs in a secure sandbox against the samples (Run) and hidden tests (Submit).

Loading editor…

Log in to run and submit your code — it's free.