Sum of Left Leaves - Problem
Given the root of a binary tree, return the sum of all left leaves.
A leaf is a node with no children. A left leaf is a leaf that is the left child of another node.
Input & Output
Example 1 — Standard Tree
$
Input:
root = [3,9,20,null,null,15,7]
›
Output:
24
💡 Note:
Left leaves are 9 (left child of 3) and 15 (left child of 20). Sum = 9 + 15 = 24
Example 2 — Single Node
$
Input:
root = [1]
›
Output:
0
💡 Note:
Root node has no parent, so it's not a left leaf. No left leaves exist.
Example 3 — Only Right Children
$
Input:
root = [1,null,2,null,3]
›
Output:
0
💡 Note:
All children are right children (2 is right child of 1, 3 is right child of 2). No left leaves.
Constraints
- The number of nodes in the tree is in the range [1, 1000]
- -1000 ≤ Node.val ≤ 1000
Visualization
Tap to expand
Understanding the Visualization
1
Input Tree
Binary tree with nodes [3,9,20,null,null,15,7]
2
Identify Left Leaves
Find leaf nodes that are left children: 9 and 15
3
Sum Values
Add up the values: 9 + 15 = 24
Key Takeaway
🎯 Key Insight: A left leaf must be both a leaf node AND the left child of its parent - track this with a boolean flag during DFS
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code