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.
Examples:
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
Solution in Python
Approach:
- Base Case:
- If the root is
None
, return 0, as there are no nodes to consider.
- If the root is
- Recursive Strategy:
- Check if the current node has a left child and if that left child is a leaf. If so, add its value to the sum.
- Recursively compute the sum of left leaves in both the left and right subtrees.
- Leaf Node Check:
- A node is a leaf if it has no left or right children (
node.left is None and node.right is None
).
- A node is a leaf if it has no left or right children (
- Implementation:
- Use a helper function or direct recursion to compute the sum of left leaves.
Python
class Solution:
def sumOfLeftLeaves(self, root: Optional[TreeNode]) -> int:
# Helper function to check if a node is a leaf
def isLeaf(node):
return node is not None and node.left is None and node.right is None
# Base case: if the root is None, return 0
if root is None:
return 0
# Initialize sum
total = 0
# Check if the left child is a leaf
if root.left and isLeaf(root.left):
total += root.left.val
# Recursively compute the sum of left leaves in the left and right subtrees
total += self.sumOfLeftLeaves(root.left)
total += self.sumOfLeftLeaves(root.right)
return total
Explanation of the Code:
isLeaf
Function:- This function checks if a given node is a leaf by ensuring it is not
None
and has no children.
- This function checks if a given node is a leaf by ensuring it is not
- Base Case:
- If the
root
isNone
, the function returns 0.
- If the
- Left Leaf Check:
- If the left child of the current node exists and is a leaf, add its value to the
total
.
- If the left child of the current node exists and is a leaf, add its value to the
- Recursive Calls:
- Recursively calculate the sum of left leaves in the left and right subtrees, and add them to the
total
.
- Recursively calculate the sum of left leaves in the left and right subtrees, and add them to the
- Return Value:
- The function returns the total sum of all left leaves.
Solution in C++
C++
class Solution {
public:
// Function to calculate the sum of all left leaves in a binary tree.
int sumOfLeftLeaves(TreeNode* root) {
// If the root is null, there are no leaves, so return 0.
if (!root) {
return 0;
}
int sum = 0;
// Check if the left child exists and if it's a leaf node.
if (root->left && !root->left->left && !root->left->right) {
// Add the value of the left leaf to the sum.
sum += root->left->val;
}
// Recursively calculate the sum of left leaves in the left and right subtrees.
sum += sumOfLeftLeaves(root->left);
sum += sumOfLeftLeaves(root->right);
return sum; // Return the total sum of left leaves.
}
};
Solution in Javascript
JavaScript
var sumOfLeftLeaves = function(root) {
// Base case: if the root is null, there is no tree, so return 0
if (!root) {
return 0;
}
let sum = 0;
// Check if the left child exists and is a leaf
if (root.left && !root.left.left && !root.left.right) {
// Add the value of the left leaf to the sum
sum += root.left.val;
}
// Recursively calculate the sum of left leaves in the left and right subtrees
sum += sumOfLeftLeaves(root.left);
sum += sumOfLeftLeaves(root.right);
// Return the total sum of left leaves
return sum;
};
Solution in Java
Java
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 left child exists and is a leaf node.
if (root.left != null) {
// A leaf node is defined as having no children (both left and right are null).
if (root.left.left == null && root.left.right == null) {
sum += root.left.val; // Add the value of the left leaf node to the sum.
}
}
// Recursively calculate the sum of left leaves in the left and right subtrees.
sum += sumOfLeftLeaves(root.left);
sum += sumOfLeftLeaves(root.right);
// Return the accumulated sum.
return sum;
}
}