Sum of Left Leaves
Easy
Topics
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
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
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
- Recurse carrying a flag
isLeft. - If the node is a leaf and
isLeftis true, add its value. - Recurse left with
isLeft=trueand right withisLeft=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)import java.util.*;
class TreeNode {
int val;
TreeNode left, right;
TreeNode(int v) {
val = v;
}
}
class Solution {
public int sumOfLeftLeaves(TreeNode root) {
int sum = 0;
Deque<TreeNode> st = new ArrayDeque<>();
Deque<Boolean> sl = new ArrayDeque<>();
if (root != null) {
st.push(root);
sl.push(false);
}
while (!st.isEmpty()) {
TreeNode n = st.pop();
boolean left = sl.pop();
if (n.left == null && n.right == null) {
if (left) sum += n.val;
continue;
}
if (n.left != null) {
st.push(n.left);
sl.push(true);
}
if (n.right != null) {
st.push(n.right);
sl.push(false);
}
}
return sum;
}
}#include <bits/stdc++.h>
using namespace std;
struct TreeNode {
int val;
TreeNode *left, *right;
TreeNode(int v): val(v), left(nullptr), right(nullptr) {
}
};
class Solution {
public: int sumOfLeftLeaves(TreeNode* root) {
function<int(TreeNode*,bool)> dfs = [&](TreeNode* node, bool left) -> int {
if (!node) return 0;
if (!node->left && !node->right) return left ? node->val : 0;
return dfs(node->left, true) + dfs(node->right, false);
};
return dfs(root, false);
}
};/**
* Tree node shape: { val, left, right }
* @param {TreeNode} root
* @return {number}
*/ var sumOfLeftLeaves = function(root) {
const dfs = (node, left) => {
if (!node) return 0;
if (!node.left && !node.right) return left ? node.val : 0;
return dfs(node.left, true) + dfs(node.right, false);
};
return dfs(root, false);
};