404. Sum of Left Leaves
Problem Description
Given the root of a binary tree, return the sum of all left leaves.
A leaf is a node with no children. A left leaf is a leaf that is the left child of another node.
Example 1:
Input: root = [3,9,20,null,null,15,7]
Output: 24
Explanation: There are two left leaves in the binary tree, with values 9 and 15 respectively.
Example 2:
Input: root = [1]
Output: 0
Constraints:
- The number of nodes in the tree is in the range
[1, 1000] -1000 <= Node.val <= 1000
Solution
Python Solution
class Solution:
def sumOfLeftLeaves(self, root: Optional[TreeNode]) -> int:
if not root:
return 0
def isLeaf(node):
return node and not node.left and not node.right
result = 0
# Check if left child is a leaf
if root.left and isLeaf(root.left):
result += root.left.val
# Recursively process left and right subtrees
result += self.sumOfLeftLeaves(root.left)
result += self.sumOfLeftLeaves(root.right)
return result
Time Complexity: O(n)
Where n is the number of nodes in the binary tree. We visit each node exactly once.
Space Complexity: O(h)
Where h is the height of the tree, for the recursion stack.
Java Solution
class Solution {
private boolean isLeaf(TreeNode node) {
return node != null && node.left == null && node.right == null;
}
public int sumOfLeftLeaves(TreeNode root) {
if (root == null) {
return 0;
}
int result = 0;
// Check if left child is a leaf
if (root.left != null && isLeaf(root.left)) {
result += root.left.val;
}
// Recursively process left and right subtrees
result += sumOfLeftLeaves(root.left);
result += sumOfLeftLeaves(root.right);
return result;
}
}
Time Complexity: O(n)
Where n is the number of nodes in the binary tree. We visit each node exactly once.
Space Complexity: O(h)
Where h is the height of the tree, for the recursion stack.
C++ Solution
class Solution {
private:
bool isLeaf(TreeNode* node) {
return node && !node->left && !node->right;
}
public:
int sumOfLeftLeaves(TreeNode* root) {
if (!root) {
return 0;
}
int result = 0;
// Check if left child is a leaf
if (root->left && isLeaf(root->left)) {
result += root->left->val;
}
// Recursively process left and right subtrees
result += sumOfLeftLeaves(root->left);
result += sumOfLeftLeaves(root->right);
return result;
}
};
Time Complexity: O(n)
Where n is the number of nodes in the binary tree. We visit each node exactly once.
Space Complexity: O(h)
Where h is the height of the tree, for the recursion stack.
JavaScript Solution
/**
* @param {TreeNode} root
* @return {number}
*/
var sumOfLeftLeaves = function(root) {
if (!root) {
return 0;
}
const isLeaf = (node) => {
return node && !node.left && !node.right;
};
let result = 0;
// Check if left child is a leaf
if (root.left && isLeaf(root.left)) {
result += root.left.val;
}
// Recursively process left and right subtrees
result += sumOfLeftLeaves(root.left);
result += sumOfLeftLeaves(root.right);
return result;
};
Time Complexity: O(n)
Where n is the number of nodes in the binary tree. We visit each node exactly once.
Space Complexity: O(h)
Where h is the height of the tree, for the recursion stack.
C# Solution
public class Solution {
private bool IsLeaf(TreeNode node) {
return node != null && node.left == null && node.right == null;
}
public int SumOfLeftLeaves(TreeNode root) {
if (root == null) {
return 0;
}
int result = 0;
// Check if left child is a leaf
if (root.left != null && IsLeaf(root.left)) {
result += root.left.val;
}
// Recursively process left and right subtrees
result += SumOfLeftLeaves(root.left);
result += SumOfLeftLeaves(root.right);
return result;
}
}
Time Complexity: O(n)
Where n is the number of nodes in the binary tree. We visit each node exactly once.
Space Complexity: O(h)
Where h is the height of the tree, for the recursion stack.
Approach Explanation
The solution uses a recursive approach to traverse the binary tree:
- Key Insights:
- Tree traversal
- Leaf detection
- Left child check
- Sum accumulation
- Algorithm Steps:
- Check root
- Identify leaves
- Process subtrees
- Accumulate sum
Implementation Details:
- Recursion usage
- Node validation
- Value accumulation
- Tree traversal
Optimization Insights:
- Early termination
- Leaf checking
- Stack optimization
- Memory usage
Edge Cases:
- Empty tree
- Single node
- No left leaves
- All left leaves