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:
[1, 1000]
.-1000 <= Node.val <= 1000
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
We need to find the sum of all the left 'leaves' in a tree. The brute force way is to look at every single part of the tree and check if it is a left leaf, and if so, add its value to the running total.
Here's how the algorithm would work step-by-step:
def sum_of_left_leaves_brute_force(root):
total_sum = 0
if root is None:
return total_sum
# Check if a left branch exists.
if root.left:
# Check if the left child is a leaf node.
if root.left.left is None and root.left.right is None:
total_sum += root.left.val
total_sum += sum_of_left_leaves_brute_force(root.left)
# Recursively call the function on the right subtree
total_sum += sum_of_left_leaves_brute_force(root.right)
return total_sum
To efficiently find the sum of left leaves, we'll use a simple tree traversal. The key is to only add the value of a node if it is both a leaf and a left child of its parent.
Here's how the algorithm would work step-by-step:
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
def sum_of_left_leaves(root: TreeNode) -> int:
total_sum = 0
def traverse_tree(node: TreeNode, is_left: bool):
nonlocal total_sum
if not node:
return
# Check if the current node is a leaf node.
if not node.left and not node.right and is_left:
total_sum += node.val
return
# Traverse to the left child.
if node.left:
traverse_tree(node.left, True)
# Traverse to the right child.
if node.right:
# Right child is never a left node.
traverse_tree(node.right, False)
# Initiate traversal from the root.
traverse_tree(root, False)
return total_sum
Case | How to Handle |
---|---|
Null or empty tree (root is null) | Return 0 immediately, as there are no nodes, and therefore no left leaves. |
Tree with only a root node | Return 0, as a single root node cannot have any children (left or otherwise). |
Tree with a root and only a left child that is a leaf | The solution should correctly identify this single left leaf and return its value. |
Tree with a root and only a right child that is a leaf | Return 0, as we are only concerned with left leaves. |
A left child that is not a leaf | The solution should traverse further down the tree and not count the value of this non-leaf node in the sum. |
Large, deeply unbalanced tree | The recursive solution should be mindful of potential stack overflow issues and consider an iterative solution using a stack if needed to prevent it. |
Tree with negative values | The solution should handle negative node values correctly, simply adding them to the sum if they are left leaves. |
Tree with a left leaf having value zero | The solution should correctly include zero in the sum when it encounters a left leaf with value zero. |