02
Intuition
A node can't know it's a left child — but its parent can. Pass a flag down: when recursing left, mark it. Add the value only when the flagged node turns out to be a leaf.
03
Approach
1
Parent tags the child
dfs(node, isLeft): if node is a leaf and isLeft, contribute its value.
2
Recurse both sides
dfs(left, True) + dfs(right, False). Right leaves contribute nothing but their subtrees may contain left leaves.
3
Leaf test
leaf = no children. A left child with a subtree is NOT a left leaf — the flag alone isn't enough.
04
Solution & live demo
python
▶1class Solution:
▶2 def sumOfLeftLeaves(self, root):
▶3 def dfs(node, is_left):
▶4 if not node: return 0
▶5 if not node.left and not node.right:
▶6 return node.val if is_left else 0
▶7 return dfs(node.left, True) + dfs(node.right, False)
▶8 return dfs(root, False)
05
Edge cases
Root only
Root is nobody's child → 0.
Left child with its own children
Not a leaf — recursion continues inside it instead of adding.
06
Complexity
Time
O(n)
Space
O(h)
Plain DFS with a boolean.