Sum of Left Leaves

Easy

Topics
TreeDFS

Given the root of a binary tree, return the sum of all left leaves — leaf nodes that are the left child of their parent.

Example 1

9315207
left leaf
Only leaves that are a left child count — being a leaf is not enough, so the recursion has to carry which side it came down. The shaded nodes total 24.
Tree given in level-order (breadth-first); null marks a missing child.
Input:  root = [3,9,20,null,null,15,7]
Output: 24
Explanation: Left leaves are 9 and 15; 9 + 15 = 24.

Example 2

1
Input:  root = [1]
Output: 0

Constraints

  • 1 <= number of nodes <= 1000
  • -1000 <= Node.val <= 1000
Editorial — intuition, approach, complexity and pitfalls

Intuition

A node contributes only if it is a left child AND a leaf. Pass down whether the current node is a left child so we know when to count it.

Approach

  1. Recurse carrying a flag isLeft.
  2. If the node is a leaf and isLeft is true, add its value.
  3. Recurse left with isLeft=true and right with isLeft=false.

Dry Run

tree 3 9 20 null null 15 7
      3
     / \
    9   20
       /  \
      15   7
The flag is passed DOWN, because a node cannot tell on its own which side of its
parent it sits on:
  node 3    not a leaf, contributes nothing itself
    leaf 9    isLeft=true   -> contributes 9
    node 20   not a leaf, contributes nothing itself
      leaf 15   isLeft=true   -> contributes 15
      leaf 7    isLeft=false  -> contributes 0
answer 9 + 15 = 24
Both conditions are needed, and this tree demonstrates one of them. Leaf 7 is a
leaf but a right child, so it is skipped; count every leaf regardless of side and
the answer becomes 31.
The other half needs a different shape, because every left child in this tree
happens to be a leaf. On 3 9 20 15 7 the 9 is a left child with children of its
own, and the answer is 15 -- only the leaf 15 qualifies. Count every left child
whether or not it is a leaf and that becomes 24, with the non-leaf 9 wrongly
included.
The root is seeded with isLeft=false, which is what stops a single-node tree from
counting its own root: on the tree "1" the answer is 0, not 1.
Note that non-contributing internal nodes still return the sum of their children.
Node 20 adds nothing of its own but passes 15 upward, so the recursion carries
totals through nodes that do not qualify.

Complexity

  • Time — O(n): each node visited once.
  • Space — O(h): recursion stack.

Common Pitfalls

  • A left child that is not a leaf does not count — check the leaf condition too.

Full Solution — all languages

class TreeNode:
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right

class Solution:
    def sumOfLeftLeaves(self, root):
        def dfs(node, isLeft):
            if not node:
                return 0
            if not node.left and not node.right:
                return node.val if isLeft else 0
            return dfs(node.left, True) + dfs(node.right, False)
        return dfs(root, False)
SensAI
Beta
Tap the mic to stop voice mode