0404. Sum of Left Leaves

404. Sum of Left Leaves #

Problem #

Find the sum of all left leaves in a given binary tree.

Example:

    3
   / \
  9  20
    /  \
   15   7

There are two left leaves in the binary tree, with values 9 and 15 respectively. Return 24.

Problem Summary #

Calculate the sum of all left leaves in a given binary tree.

Solution Approach #

  • This problem is a Microsoft interview question. It can be solved recursively.

Code #


package leetcode

/**
 * 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
	}
	if root.Left != nil && root.Left.Left == nil && root.Left.Right == nil {
		return root.Left.Val + sumOfLeftLeaves(root.Right)
	}
	return sumOfLeftLeaves(root.Left) + sumOfLeftLeaves(root.Right)
}


Calendar Jun 25, 2026
Edit Edit this page
Total visits:   You are visitor No.
中文