Sum of Left Leaves
Sum of Left Leaves is LeetCode 404 (Easy). Given the root of a binary tree, return the sum of the values of all its left leaves.
- A leaf is a node with no children.
- A left leaf is a leaf that is the left child of its parent.
Values can be negative, so the answer can be negative too. A tree with no left leaf returns 0.
- The number of nodes in the tree is in the range [1, 1000].
- -1000 <= Node.val <= 1000
Intuition
A node can see whether it is a leaf: it checks its own children. It cannot see whether it is a left child, because only its parent knows which link led to it. So let the parent pass that fact down as a flag: True into the left child, False into the right child.
- Leaf with the flag set: a left leaf, so return its value.
- Leaf without the flag: a right leaf or the root, so return 0.
- Any other node: return the sum of what its two children return.
Every left leaf lies somewhere below the root, so visiting every node once and adding up the returns finds them all.
When a node's answer depends on how you reached it, pass that fact down as an argument. Path Sum passes the remaining target and Binary Tree Paths passes the path so far in the same way.
Approach
Before reading on, decide what a node can tell about itself and what only its parent knows. Then decide how that missing fact can reach the node. Aim for O(n) time.
Pass a left flag down
Define dfs(node, is_left). The caller sets the flag: True when it steps into a left child and False for a right child. The first call is dfs(root, False), since the root is nobody's child.
Return 0 for an empty spot
dfs(None, ...) returns 0, which adds nothing to the sum. A node with only one child can then call both sides without first checking which one exists, so the recursive line is the same for every node.
Decide at a leaf
If the node has no left and no right child, return node.val when is_left is true and 0 otherwise. This is the only place a value enters the sum.
Add up both sides
Any other node returns dfs(node.left, True) + dfs(node.right, False). The right side is searched too, because a right child can have left leaves of its own.
Code in Python, C++ and Java
Common pitfalls
Adding every left child
if node.left:
total += node.left.valif not node.left and not node.right:
return node.val if is_left else 0A left child with children of its own is not a leaf. On [1,2,null,null,3] this adds 2, but 2 has a right child, and 3 is a right leaf, so the answer is 0.
Starting the root as a left child
return dfs(root, True)
return dfs(root, False)
The root has no parent, so it is never a left leaf. On the single-node tree [1] this returns 1 instead of 0.
Reading a grandchild before checking the child exists
if not root.left.left and not root.left.right:
total += root.left.valif root.left and not root.left.left and not root.left.right:
total += root.left.valIn the version where a parent tests its left child, [1,null,2] has no left child, so root.left.left raises AttributeError (a null-pointer crash in C++ or Java). Check root.left first.
Edge cases
The recursion goes 1,000 calls deep, just past Python's default limit of 1,000, so the code raises it with sys.setrecursionlimit. C++ and Java handle that depth with no change.
Complexity
Sum of Left Leaves FAQ
Can it be done without recursion?
Keep a stack of (node, is_left) pairs, starting with (root, False). Pop a pair; if the node is a leaf and the flag is true, add its value; otherwise push its children, the left one with True and the right one with False. It is still O(n), with no recursion limit to raise.
How would you sum the right leaves instead?
Swap the flags: pass True to the right child and False to the left one, and keep the root at False. To sum every leaf, drop the flag and return node.val at each leaf.