AAlgoLoopSpaced repetition for LeetCode
EASYTree / RecursionLeetCode ↗

Sum of Left Leaves

The key idea

A node only knows it is a left leaf if its parent tells it. So pass an isLeft flag down to each child, and add a node's value only when it has no children AND it arrived as a left child.

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.

Constraints

Examples

Input: root = [3,9,20,null,null,15,7] Output: 24
Input: root = [1] Output: 0

Complexity

Time: O(n) Space: O(h)

See the full solution

410310
Step-by-step visualization
Start free →

More Tree / Recursion problems