# 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: TreeNode) -> int:
if not root:
return 0
def is_leaf(node):
return node and not node.left and not node.right
total = 0
if root.left:
if is_leaf(root.left):
total += root.left.val
else:
total += self.sumOfLeftLeaves(root.left)
if root.right:
total += self.sumOfLeftLeaves(root.right)
return total
// Definition for a binary tree node.
// struct TreeNode {
// int val;
// TreeNode *left;
// TreeNode *right;
// TreeNode(int x) : val(x), left(NULL), right(NULL) {}
// };
class Solution {
public:
int sumOfLeftLeaves(TreeNode* root) {
if (!root) return 0;
int total = 0;
if (root->left) {
if (!root->left->left && !root->left->right)
total += root->left->val;
else
total += sumOfLeftLeaves(root->left);
}
if (root->right)
total += sumOfLeftLeaves(root->right);
return total;
}
};
// Definition for a binary tree node.
// public class TreeNode {
// int val;
// TreeNode left;
// TreeNode right;
// TreeNode(int x) { val = x; }
// }
class Solution {
public int sumOfLeftLeaves(TreeNode root) {
if (root == null) return 0;
int total = 0;
if (root.left != null) {
if (root.left.left == null && root.left.right == null)
total += root.left.val;
else
total += sumOfLeftLeaves(root.left);
}
if (root.right != null)
total += sumOfLeftLeaves(root.right);
return total;
}
}
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @return {number}
*/
var sumOfLeftLeaves = function(root) {
if (!root) return 0;
let total = 0;
if (root.left) {
if (!root.left.left && !root.left.right)
total += root.left.val;
else
total += sumOfLeftLeaves(root.left);
}
if (root.right)
total += sumOfLeftLeaves(root.right);
return total;
};
Given the root
of a binary tree, your task is to find the sum of all left leaves. A left leaf is a node that is the left child of its parent and does not have any children (i.e., both its left
and right
pointers are null
). The tree can have any structure, and you need to return the sum of the values of all such left leaves.
To solve this problem, we first need to understand what constitutes a "left leaf." We are only interested in nodes that:
We also need to decide between recursion and iteration. Recursion is a natural fit for tree problems, as it allows us to handle each subtree in a similar way. We'll also consider how to avoid double-counting or missing any left leaves.
We'll use a recursive approach to traverse the tree. Here’s how we can break down the solution:
null
, return 0. This handles empty trees and recursion end points.
left
and right
are null
).
This approach ensures that every left leaf is counted exactly once, and the function visits each node only as needed.
Let's consider the following binary tree:
3 / \ 9 20 / \ 15 7
The sum of left leaves is 9 + 15 = 24
.
Step-by-step:
Brute-force approach: Traversing every node and checking if it is a left leaf would take O(n) time, where n is the number of nodes. There is no way to do better since each node must be checked at least once.
Optimized recursive approach: Still O(n) time, as every node is visited once. Space complexity is O(h), where h is the height of the tree, due to the call stack in recursion. For a balanced tree, h = log n; for a skewed tree, h = n.
The "Sum of Left Leaves" problem is a classic binary tree traversal challenge. By understanding the definition of a left leaf and using recursion, we efficiently sum the values of all left leaves. The solution is elegant because it leverages the natural structure of trees and recursion, ensuring each node is visited only as necessary. The approach is both simple and optimal for time and space complexity.