Sum of Left Leaves (LeetCode 404): Solution and Explanation

Easy
Tree
Depth First Search
Breadth First Search
Binary Tree

Given the root of a binary tree, return the sum of the values of all left leaves. A left leaf is a node with no children that is the left child of its parent.

Variables
No variables to display
DepthFunction Call
Stack empty
0/0

Intuition

Summing all leaves is a standard traversal: visit every node and add its value when it has no children. The twist here is the word left, and that information is not stored in the node itself. A leaf has no idea whether it hangs off its parent's left or right side. So the decision has to be made somewhere that knows the direction.

There are two natural places to put it. One is to pass a flag down the recursion saying whether the current node is a left child. The other, which the reference code uses, is to make the decision at the parent. When you stand on a node, you can look at root.left and ask whether it is a leaf. If it is, its value counts and there is nothing below it to explore. If it is not a leaf, you recurse into it normally, since left leaves may exist deeper in that subtree.

The right child is never a left leaf itself, but its subtree can contain left leaves, so you always recurse into it and add whatever it returns. Each node is handled with constant work beyond its recursive calls, so the traversal visits every node once. One edge case falls out naturally: a tree with a single node has no left leaves, because the root is not anyone's left child, and the code correctly returns 0.

Approach: Recursive DFS deciding at the parent

  1. If root is None, return 0.
  2. Start a local total at 0.
  3. If root.left exists and has no children, add root.left.val to total because it is a left leaf.
  4. If root.left exists but has children, add the result of recursing into root.left instead.
  5. Always add the result of recursing into root.right, which handles any left leaves deeper on the right side.
  6. Return total.

Step-by-step example

root = [3, 9, 20, null, null, 15, 7]
  1. At the root 3, the left child 9 has no children, so it is a left leaf and total becomes 9.
  2. Recurse into the right child 20. Its left child 15 has no children, so 20's total starts at 15.
  3. From 20, recurse into its right child 7. Node 7 has no left child and its right recursion returns 0, so it returns 0.
  4. Node 20 returns 15 + 0 = 15.
  5. Back at the root, total is 9 + 15 = 24, which is the answer. Node 7 is a leaf but it is a right child, so it does not count.

Code: Python, Java, C++

1class Solution:
2    def sumOfLeftLeaves(self, root: Optional[TreeNode]) -> int:
3        if not root:
4            return 0
5
6        total = 0
7        if root.left:
8            if not root.left.left and not root.left.right:
9                total += root.left.val  # Left leaf found
10            else:
11                total += self.sumOfLeftLeaves(root.left)
12
13        total += self.sumOfLeftLeaves(root.right)
14
15        return total

Time and space complexity

Time

O(n)

Each node is entered at most once by the recursion and does constant work. Left leaves are read directly from their parent without an extra call, which only saves time.

Space

O(h)

The recursion stack grows to the height h of the tree. That is O(log n) for a balanced tree and O(n) for a tree shaped like a chain.

Other approaches

Iterative BFS or DFS (time O(n), space O(n))

Push the root onto a queue or stack. When popping a node, check whether its left child is a leaf and add its value if so; otherwise push the left child. Always push the right child when it exists. It avoids recursion depth limits on very skewed trees.

Recursion with an is-left flag (time O(n), space O(h))

Call a helper with (node, is_left). If the node is a leaf, return its value when is_left is true and 0 otherwise; if not, return the sum of the helper on its left child with true and its right child with false. Some people find this easier to read because each node decides for itself.

Common mistakes

  • Counting the root when the tree has one node. The root has no parent, so it is not a left leaf and the answer is 0.
  • Adding every left child rather than only left children that are leaves. A left child with its own children contributes only through its descendants.
  • Skipping recursion into the right subtree because right children cannot be left leaves. Their descendants still can be.
  • Checking only root.left.left to decide whether the left child is a leaf. You must confirm both left and right of that child are None.

Frequently asked questions

Is the root counted as a left leaf?

No. A left leaf must be the left child of some parent, and the root has no parent, so a single-node tree returns 0.

How do you know a leaf is a left leaf?

A node does not store which side it is on, so the check has to happen at the parent, or you pass an is-left flag down the recursion.

Can Sum of Left Leaves be solved iteratively?

Yes. Use a stack or queue and, when processing a node, check whether its left child is a leaf before deciding whether to push it. The logic is the same as the recursive version.