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
Sum of Left Leaves Problem3920157Left Leaf: 9Left Leaf: 15Right Leaf: 7Sum of Left Leaves = 9 + 15 = 24
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
Asked in
Facebook 15 Amazon 12 Google 8
180.0K Views
Medium Frequency
~15 min Avg. Time
2.1K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen