Knowledge Guide
HomeDSATrees

easy Sum of Left Leaves

Problem Statement

Given the root of a binary tree, return the sum of all left leaves.

A leaf is a node that does not have any child nodes, and a left leaf is a leaf that is the left child of its parent.

Examples

Example 1:

Image
Image

Example 2:

Image
Image

Example 3:

Image
Image

Constraints:

Try it yourself

Try solving this question here:

✅ Solution Sum of Left Leaves

Problem Statement

Given the root of a binary tree, return the sum of all left leaves.

A leaf is a node that does not have any child nodes, and a left leaf is a leaf that is the left child of its parent.

Examples

Example 1:

  • Input: root = [3,5,10,null,null,8,7]
Image
Image
  • Expected Output: 13
  • Justification: The leaf nodes are 5, 8, and 7, but only 5 and 8 are left leaf nodes. So, sum of left leaf nodes are 5 + 8 = 13.

Example 2:

  • Input: root = [5, 3, 8, 2, 4, null, 6, 1]
Image
Image
  • Expected Output: 1
  • Justification: The only left leaf node is 1 (left child of 2). So, the answer is 1.

Example 3:

  • Input: root = [1, null, 5, null, 4]
Image
Image
  • Expected Output: 0
  • Justification: The tree doesn't have any left leaf node.

Constraints:

  • The number of nodes in the tree is in the range [1, 1000].
  • -1000 <= Node.val <= 1000

Solution

To solve this problem, we use Depth-First Search (DFS) to traverse the binary tree and find all left leaf nodes. Starting from the root, we recursively explore each node's left and right children. If a node is a leaf (it has no children) and is a left child (tracked using a boolean flag), we add its value to the total sum. For every node, we check its left child by setting the flag as true and its right child with the flag as false. This approach ensures that only left leaf nodes are considered in the sum, and we continue this process until all nodes are visited.

Step-by-Step Algorithm

Start from the root node:

  • Call dfs with the root node and isLeft set to false to indicate it is not a left child.

DFS() Function

  1. Check for a null node:

    • If the current node is null, return 0. This means there are no further nodes in this path.
  2. Identify a left leaf node:

    • If the current node is a leaf (node.left == null && node.right == null) and isLeft is true, return node.val. This adds the value of the left leaf node to the sum.
  3. Recursive traversal for children:

    • Call dfs recursively for the left child (node.left) with isLeft set to true to track it as a left child.
    • Call dfs recursively for the right child (node.right) with isLeft set to false.
  4. Calculate total sum:

    • Return the sum of the results from the left and right child calls to accumulate the total sum of all left leaves in the tree.

Algorithm Walkthrough

Input: root = [3, 5, 10, null, null, 8, 7]:

Image
Image

Start with the root node:

  • The root node is 3. Call dfs(3, false).
  • node is not null, so proceed further.

DFS() Function

  1. Check if root node is a left leaf:

    • node 3 is not a leaf (it has left and right children). Move to the next steps.
  2. Recursive call for the left child of root (3):

    • Call dfs(5, true).
    • node 5 is not null, so proceed further.
    • node 5 is a leaf node (node.left == null && node.right == null) and isLeft is true.
    • Return 5 (its value) as it is a left leaf.
  3. Recursive call for the right child of root (3):

    • Call dfs(10, false).
    • node 10 is not null, so proceed further.
    • node 10 is not a leaf node (it has left and right children), so move to the next steps.
  4. Recursive call for the left child of node (10):

    • Call dfs(8, true).
    • node 8 is not null, so proceed further.
    • node 8 is a leaf node (node.left == null && node.right == null) and isLeft is true.
    • Return 8 (its value) as it is a left leaf.
  5. Recursive call for the right child of node (10):

    • Call dfs(7, false).
    • node 7 is not null, so proceed further.
    • node 7 is a leaf node, but isLeft is false.
    • Return 0 as it is not a left leaf.
  6. Combine results for node (10):

    • The result for node 10 is dfs(8, true) + dfs(7, false), which is 8 + 0 = 8.
    • Return 8.
  7. Combine results for root node (3):

    • The result for the root node 3 is dfs(5, true) + dfs(10, false), which is 5 + 8 = 13.
    • Return 13.
  8. Final result:

    • The total sum of left leaves in the tree [3,5,10,null,null,8,7] is 13.

Code

java
// class TreeNode {
//     int val;
//     TreeNode left;
//     TreeNode right;

//     TreeNode(int val) {
//         this.val = val;
//     }
// }

public class Solution {

  public int sumOfLeftLeaves(TreeNode root) {
    return dfs(root, false);
  }

  private int dfs(TreeNode node, boolean isLeft) {
    // If the node is null, return 0 (base case)
    if (node == null) return 0;

    // If the node is a leaf and is a left child, return its value
    if (node.left == null && node.right == null && isLeft) return node.val;

    // Recursively calculate the sum for left and right children
    return dfs(node.left, true) + dfs(node.right, false);
  }

  // Main method to test the function with examples
  public static void main(String[] args) {
    Solution solution = new Solution();

    // Example 1: Create the tree [3,5,10,null,null,8,7]
    TreeNode root1 = new TreeNode(3);
    root1.left = new TreeNode(5);
    root1.right = new TreeNode(10);
    root1.right.left = new TreeNode(8);
    root1.right.right = new TreeNode(7);

    System.out.println(
      "Sum of left leaves (Example 1): " + solution.sumOfLeftLeaves(root1)
    ); // Expected output: 13
  }
}

Complexity Analysis

Time Complexity

  • The time complexity of the above solution is , where n is the number of nodes in the binary tree.
  • This is because the algorithm visits each node exactly once in a Depth-First Search (DFS) manner to determine whether it is a left leaf.

Space Complexity

  • The space complexity of the solution is , where h is the height of the binary tree.
  • This is due to the recursion stack used in the DFS. In the worst case, if the tree is completely unbalanced, the recursion stack would go up to , but in a balanced binary tree, it would be .
🤖 Don't fully get this? Learn it with Claude

Stuck on Sum of Left Leaves? Open Claude, copy a block below, and it'll teach you this exact concept — visually and interactively.

🪜 Hint ladder (no spoilers)

Progressively stronger hints — you still solve it.

I'm working on the problem **Sum of Left Leaves** (DSA). Give me a HINT LADDER: start with the tiniest nudge, then wait. Only reveal the next, stronger hint when I ask. Do NOT show the full solution unless I type 'show solution'. Keep me doing the thinking. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🎨 Explain the approach visually

See the technique, not just code.

Explain the optimal approach to **Sum of Left Leaves** with a VISUAL walkthrough: trace it on a small concrete example using ASCII art / a step-by-step diagram, narrate what changes each step, then give time & space complexity with a one-line derivation. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🔍 Review my solution

Catch bugs, edge cases, sub-optimality.

I'll paste my solution to **Sum of Left Leaves**. Review it for correctness, missed edge cases, and time/space complexity, then coach me toward the optimal — don't just rewrite it. Ask me to paste my code now. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🔁 Drill the pattern

Lock in recognition with look-alikes.

Give me 2 problems that use the SAME underlying pattern as **Sum of Left Leaves**. For each, let me attempt first, then review my answer and name the trigger signal that reveals the pattern. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.

📝 My notes