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:
- Input: root =
[3,5,10,null,null,8,7]
- Expected Output:
13 - Justification: The leaf nodes are
5,8, and7, but only5and8are 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]
- Expected Output:
1 - Justification: The only left leaf node is
1(left child of2). So, the answer is 1.
Example 3:
- Input: root =
[1, null, 5, null, 4]
- 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
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]
- Expected Output:
13 - Justification: The leaf nodes are
5,8, and7, but only5and8are 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]
- Expected Output:
1 - Justification: The only left leaf node is
1(left child of2). So, the answer is 1.
Example 3:
- Input: root =
[1, null, 5, null, 4]
- 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
dfswith the root node andisLeftset tofalseto indicate it is not a left child.
DFS() Function
-
Check for a null node:
- If the current
nodeisnull, return0. This means there are no further nodes in this path.
- If the current
-
Identify a left leaf node:
- If the current
nodeis a leaf (node.left == null && node.right == null) andisLeftistrue, returnnode.val. This adds the value of the left leaf node to the sum.
- If the current
-
Recursive traversal for children:
- Call
dfsrecursively for the left child (node.left) withisLeftset totrueto track it as a left child. - Call
dfsrecursively for the right child (node.right) withisLeftset tofalse.
- Call
-
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]:
Start with the root node:
- The root node is
3. Calldfs(3, false). nodeis notnull, so proceed further.
DFS() Function
-
Check if root node is a left leaf:
node3is not a leaf (it has left and right children). Move to the next steps.
-
Recursive call for the left child of root (3):
- Call
dfs(5, true). node5is notnull, so proceed further.node5is a leaf node (node.left == null && node.right == null) andisLeftistrue.- Return
5(its value) as it is a left leaf.
- Call
-
Recursive call for the right child of root (3):
- Call
dfs(10, false). node10is notnull, so proceed further.node10is not a leaf node (it has left and right children), so move to the next steps.
- Call
-
Recursive call for the left child of node (10):
- Call
dfs(8, true). node8is notnull, so proceed further.node8is a leaf node (node.left == null && node.right == null) andisLeftistrue.- Return
8(its value) as it is a left leaf.
- Call
-
Recursive call for the right child of node (10):
- Call
dfs(7, false). node7is notnull, so proceed further.node7is a leaf node, butisLeftisfalse.- Return
0as it is not a left leaf.
- Call
-
Combine results for node (10):
- The result for
node10isdfs(8, true) + dfs(7, false), which is8 + 0 = 8. - Return
8.
- The result for
-
Combine results for root node (3):
- The result for the root node
3isdfs(5, true) + dfs(10, false), which is5 + 8 = 13. - Return
13.
- The result for the root node
-
Final result:
- The total sum of left leaves in the tree
[3,5,10,null,null,8,7]is13.
- The total sum of left leaves in the tree
Code
// 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 nis 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 his 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.
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.
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.
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.
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.