expertfunda logo Top Interview Questions Binary Tree Binary Search Tree Graph

Sum of Left Leaves in Binary Tree

Sum of Left Leaves in Binary Tree

Sum of Left Leaves in B-Tree

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.


Leetcode 404. Sum of Left Leaves(Recursive and Iterative Approaches) || Must Watch | FAANG Asked

See Video lecture


Leetcode 404 Sum of Left Leaves in Binary Tree

Binary trees are one of the most important data structures in computer science. They are widely used in algorithms, data organization, and hierarchical systems. One of the common problems in binary trees is calculating the **sum of left leaves**. A **left leaf** is a node that is the left child of its parent and has no children of its own.

This problem is an excellent exercise in tree traversal and understanding the structure of binary trees.

Problem Statement

Given the root of a binary tree, calculate the sum of all left leaf nodes. If the tree is empty or there are no left leaves, the result should be 0.

For example, consider the following binary tree:

               3
              / \
             9  20
                /  \
               15   7
        

The left leaves are **9** and **15**. Therefore, the sum of left leaves is **9 + 15 = 24**.

Approaches to Solve

There are two main approaches to solve this problem:

  • Recursive Solution: Use recursion to traverse the tree and calculate the sum of left leaves.
  • Iterative Solution: Use a stack or queue to traverse the tree iteratively while keeping track of left leaves.

Recursive Solution

The recursive approach leverages the natural structure of the binary tree. At each node, we check if its left child is a leaf. If it is, we add its value to the sum. We then recursively traverse the left and right subtrees.

Algorithm:

  1. If the root is null, return 0.
  2. If the left child is a leaf, add its value to the sum and recursively calculate the sum of the right subtree.
  3. Otherwise, recursively calculate the sum of both subtrees.

function sumOfLeftLeaves(root):
    if root is null:
        return 0

    sum = 0
    if root.left is not null and root.left.left is null and root.left.right is null:
        sum += root.left.value

    return sum + sumOfLeftLeaves(root.left) + sumOfLeftLeaves(root.right)
        

Iterative Solution

The iterative approach uses a stack or queue to traverse the tree. During traversal, we check if the left child of each node is a leaf. If it is, we add its value to the sum.

Algorithm:

  1. Initialize a stack (or queue) with the root node.
  2. While the stack is not empty:
    • Pop the top node from the stack.
    • If its left child is a leaf, add its value to the sum.
    • Push the left and right children of the current node to the stack (if they exist).

function sumOfLeftLeaves(root):
    if root is null:
        return 0

    stack = [root]
    sum = 0

    while stack:
        current = stack.pop()
        if current.left is not null:
            if current.left.left is null and current.left.right is null:
                sum += current.left.value
            else:
                stack.append(current.left)

        if current.right is not null:
            stack.append(current.right)

    return sum
        

Examples

Let's consider a more complex example:

                   10
                 /    \
               5       15
              / \     /  \
             3   7   12   20
        

The left leaves are **3** and **12**. The sum of left leaves is **3 + 12 = 15**.

Recursive solution: Starting from the root, traverse each subtree, checking for left leaves.

Iterative solution: Use a stack or queue to process nodes, adding the values of left leaves when encountered.

Complexity Analysis

Time Complexity:

  • Both the recursive and iterative approaches visit each node once, resulting in a time complexity of **O(n)**, where **n** is the number of nodes in the tree.

Space Complexity:

  • Recursive solution: Space complexity is **O(h)**, where **h** is the height of the tree (due to the call stack).
  • Iterative solution: Space complexity is **O(n)** in the worst case, when all nodes are stored in the stack or queue.

Applications

Calculating the sum of left leaves has several practical applications, including:

  • Analyzing hierarchical data structures, such as organizational charts or file systems.
  • Solving problems related to leaf node classification in computational biology.
  • Debugging and testing binary tree algorithms by verifying specific properties of leaves.

Conclusion

The problem of calculating the sum of left leaves in a binary tree is a great way to practice tree traversal techniques and understand the structure of binary trees. By mastering both recursive and iterative approaches, you can solve similar problems with confidence and efficiency. This knowledge is crucial for technical interviews and real-world applications involving hierarchical data.