KatInterview

404. Sum of Left Leaves

EasySolutions in Python, Java, C++, TypeScript

Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.

Approach

The problem asks us to calculate the sum of values of all "left leaves" in a binary tree. A key distinction is made between a general leaf and a left leaf:

Solution

Sum of Left Leaves in Python

# 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:
        # Base case: if the tree is empty, return 0.
        if not root:
            return 0

        current_sum = 0
        
        # Check if the left child is a leaf node.
        # A node is a leaf if it has no children.
        if root.left and not root.left.left and not root.left.right:
            current_sum += root.left.val
        
        # The total sum is composed of this node's left leaf (if any) plus
        # the sum of left leaves from its left and right subtrees.
        return current_sum + self.sumOfLeftLeaves(root.left) + self.sumOfLeftLeaves(root.right)

Sum of Left Leaves in Java

/**
 * 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) {
        // Base case: if the tree is empty, return 0.
        if (root == null) {
            return 0;
        }

        int sum = 0;

        // Check if the current node's left child is a leaf.
        // A node is a leaf if both its left and right children are null.
        if (root.left != null && root.left.left == null && root.left.right == null) {
            sum += root.left.val;
        }

        // Recursively find the sum of left leaves in the left and right subtrees.
        sum += sumOfLeftLeaves(root.left);
        sum += sumOfLeftLeaves(root.right);

        return sum;
    }
}

Sum of Left Leaves in C++

/**
 * 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) {
        // Base case: if the tree is empty, there are no left leaves.
        if (!root) {
            return 0;
        }

        int sum = 0;
        
        // Check if the current node's left child is a leaf.
        // A node is a leaf if it has no children.
        if (root->left && !root->left->left && !root->left->right) {
            sum += root->left->val;
        }

        // Recursively find the sum of left leaves in the left and right subtrees.
        // The call on root->left explores the left subtree for more left leaves,
        // it does not re-evaluate root->left itself as a left leaf.
        sum += sumOfLeftLeaves(root->left);
        sum += sumOfLeftLeaves(root->right);

        return sum;
    }
};

Sum of Left Leaves in TypeScript

/**
 * Definition for a binary tree node.
 * class TreeNode {
 *     val: number
 *     left: TreeNode | null
 *     right: TreeNode | null
 *     constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
 *         this.val = (val===undefined ? 0 : val)
 *         this.left = (left===undefined ? null : left)
 *         this.right = (right===undefined ? null : right)
 *     }
 * }
 */

function sumOfLeftLeaves(root: TreeNode | null): number {
    // Base case: if the tree is empty, return 0.
    if (!root) {
        return 0;
    }

    let sum = 0;

    // Check if the current node's left child is a leaf.
    // A node is a leaf if both its left and right children are null.
    if (root.left && !root.left.left && !root.left.right) {
        sum += root.left.val;
    }

    // Recursively find the sum of left leaves in the left and right subtrees.
    sum += sumOfLeftLeaves(root.left);
    sum += sumOfLeftLeaves(root.right);

    return sum;
};

Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.

LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.