Sum of Left Leaves (LeetCode 404): Solution and Explanation
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.
Watch it run step by step
Try your own input and write the code →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
- If
rootis None, return 0. - Start a local
totalat 0. - If
root.leftexists and has no children, addroot.left.valtototalbecause it is a left leaf. - If
root.leftexists but has children, add the result of recursing intoroot.leftinstead. - Always add the result of recursing into
root.right, which handles any left leaves deeper on the right side. - Return
total.
Step-by-step example
root = [3, 9, 20, null, null, 15, 7]
- At the root 3, the left child 9 has no children, so it is a left leaf and
totalbecomes 9. - Recurse into the right child 20. Its left child 15 has no children, so 20's
totalstarts at 15. - From 20, recurse into its right child 7. Node 7 has no left child and its right recursion returns 0, so it returns 0.
- Node 20 returns 15 + 0 = 15.
- Back at the root,
totalis 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 total1class Solution {
2 public int sumOfLeftLeaves(TreeNode root) {
3 if (root == null) return 0;
4
5 int sum = 0;
6 if (root.left != null) {
7 if (root.left.left == null && root.left.right == null) {
8 sum += root.left.val; // Left leaf found
9 } else {
10 sum += sumOfLeftLeaves(root.left);
11 }
12 }
13 sum += sumOfLeftLeaves(root.right);
14
15 return sum;
16 }
17}1class Solution {
2public:
3 int sumOfLeftLeaves(TreeNode* root) {
4 if (!root) return 0;
5
6 int sum = 0;
7 if (root->left) {
8 if (!root->left->left && !root->left->right) {
9 sum += root->left->val; // Left leaf found
10 } else {
11 sum += sumOfLeftLeaves(root->left);
12 }
13 }
14 sum += sumOfLeftLeaves(root->right);
15
16 return sum;
17 }
18};Time and space complexity
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.
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.leftto decide whether the left child is a leaf. You must confirm bothleftandrightof 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.