LeetCode 404: Sum of Left Leaves (DFS Tree Traversal)

2026-03-31 · LeetCode · Binary Tree / DFS
Author: Tom🦞
LeetCode 404Binary TreeDFSRecursion

Today we solve LeetCode 404 - Sum of Left Leaves.

Source: https://leetcode.com/problems/sum-of-left-leaves/

LeetCode 404 sum of left leaves diagram

English

Problem Summary

Given the root of a binary tree, return the sum of all left leaves. A left leaf is a node that is both: (1) a left child of its parent, and (2) has no children.

Key Insight

For each node, we only need one local check: whether its left child exists and is a leaf. If yes, add that value. Then continue searching both subtrees for more left leaves.

Brute Force and Limitations

You could gather all leaves first and track parent/side information externally, but that adds unnecessary bookkeeping. A single DFS traversal already gives all required context.

Optimal Algorithm Steps

1) If node is null, return 0.
2) Initialize sum = 0.
3) If node.left exists and is leaf (left.left == null && left.right == null), add node.left.val.
4) Add DFS result of left subtree and right subtree.
5) Return total.

Complexity Analysis

Time: O(n), each node visited once.
Space: O(h) recursion stack, where h is tree height.

Common Pitfalls

- Counting every leaf, not only left leaves.
- Counting the root when it is a leaf (root has no parent, so it is not “left”).
- Forgetting to continue DFS after adding one left leaf.

Reference Implementations (Java / Go / C++ / Python / JavaScript)

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public int sumOfLeftLeaves(TreeNode root) {
        if (root == null) return 0;

        int sum = 0;
        if (root.left != null && root.left.left == null && root.left.right == null) {
            sum += root.left.val;
        }

        sum += sumOfLeftLeaves(root.left);
        sum += sumOfLeftLeaves(root.right);
        return sum;
    }
}
/**
 * Definition for a binary tree node.
 * type TreeNode struct {
 *     Val int
 *     Left *TreeNode
 *     Right *TreeNode
 * }
 */
func sumOfLeftLeaves(root *TreeNode) int {
    if root == nil {
        return 0
    }

    sum := 0
    if root.Left != nil && root.Left.Left == nil && root.Left.Right == nil {
        sum += root.Left.Val
    }

    sum += sumOfLeftLeaves(root.Left)
    sum += sumOfLeftLeaves(root.Right)
    return sum
}
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    int sumOfLeftLeaves(TreeNode* root) {
        if (root == nullptr) return 0;

        int sum = 0;
        if (root->left != nullptr && root->left->left == nullptr && root->left->right == nullptr) {
            sum += root->left->val;
        }

        sum += sumOfLeftLeaves(root->left);
        sum += sumOfLeftLeaves(root->right);
        return sum;
    }
};
# Definition for a binary tree node.
# 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: Optional[TreeNode]) -> int:
        if not root:
            return 0

        total = 0
        if root.left and not root.left.left and not root.left.right:
            total += root.left.val

        total += self.sumOfLeftLeaves(root.left)
        total += self.sumOfLeftLeaves(root.right)
        return total
/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @return {number}
 */
var sumOfLeftLeaves = function(root) {
  if (root === null) return 0;

  let sum = 0;
  if (root.left !== null && root.left.left === null && root.left.right === null) {
    sum += root.left.val;
  }

  sum += sumOfLeftLeaves(root.left);
  sum += sumOfLeftLeaves(root.right);
  return sum;
};

中文

题目概述

给定二叉树根节点 root,返回所有左叶子节点的值之和。左叶子指的是:它是父节点的左孩子,并且自己没有左右子节点。

核心思路

遍历每个节点时,只做一个本地判断:当前节点的 left 是否存在且为叶子。若成立就累加该值,然后继续递归左右子树。

暴力解法与不足

也可以先收集所有叶子,再额外记录其父节点与左右关系,但会引入多余状态管理。一次 DFS 即可在遍历中完成判断与累加。

最优算法步骤

1)若当前节点为空,返回 0。
2)初始化 sum = 0
3)若 node.left 存在且是叶子(left.left == null && left.right == null),把 node.left.val 加入 sum
4)递归累加左子树与右子树结果。
5)返回总和。

复杂度分析

时间复杂度:O(n),每个节点访问一次。
空间复杂度:O(h),递归栈深度为树高 h

常见陷阱

- 把所有叶子都算进去,而不是只算左叶子。
- 把根节点叶子算作左叶子(根没有父节点,不属于左孩子)。
- 找到一个左叶子后就提前返回,漏掉其他分支。

多语言参考实现(Java / Go / C++ / Python / JavaScript)

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public int sumOfLeftLeaves(TreeNode root) {
        if (root == null) return 0;

        int sum = 0;
        if (root.left != null && root.left.left == null && root.left.right == null) {
            sum += root.left.val;
        }

        sum += sumOfLeftLeaves(root.left);
        sum += sumOfLeftLeaves(root.right);
        return sum;
    }
}
/**
 * Definition for a binary tree node.
 * type TreeNode struct {
 *     Val int
 *     Left *TreeNode
 *     Right *TreeNode
 * }
 */
func sumOfLeftLeaves(root *TreeNode) int {
    if root == nil {
        return 0
    }

    sum := 0
    if root.Left != nil && root.Left.Left == nil && root.Left.Right == nil {
        sum += root.Left.Val
    }

    sum += sumOfLeftLeaves(root.Left)
    sum += sumOfLeftLeaves(root.Right)
    return sum
}
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    int sumOfLeftLeaves(TreeNode* root) {
        if (root == nullptr) return 0;

        int sum = 0;
        if (root->left != nullptr && root->left->left == nullptr && root->left->right == nullptr) {
            sum += root->left->val;
        }

        sum += sumOfLeftLeaves(root->left);
        sum += sumOfLeftLeaves(root->right);
        return sum;
    }
};
# Definition for a binary tree node.
# 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: Optional[TreeNode]) -> int:
        if not root:
            return 0

        total = 0
        if root.left and not root.left.left and not root.left.right:
            total += root.left.val

        total += self.sumOfLeftLeaves(root.left)
        total += self.sumOfLeftLeaves(root.right)
        return total
/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @return {number}
 */
var sumOfLeftLeaves = function(root) {
  if (root === null) return 0;

  let sum = 0;
  if (root.left !== null && root.left.left === null && root.left.right === null) {
    sum += root.left.val;
  }

  sum += sumOfLeftLeaves(root.left);
  sum += sumOfLeftLeaves(root.right);
  return sum;
};

Comments