Grokking Tree Coding Patterns for Interviews
Ask Author
Back to course home

0% completed

Vote For New Content
Solution: Sum of Left Leaves
Table of Contents

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Problem Statement

Given the root of a binary tree, return the sum of all left leaves.

A leaf is a node that does not have any child nodes, and a left leaf is a leaf that is the left child of its parent.

Examples

Example 1:

  • Input: root = [3,5,10,null,null,8,7]
Image
  • Expected Output: 13
  • Justification: The leaf nodes are 5, 8, and 7, but only 5 and 8 are left leaf nodes. So, sum of left leaf nodes are 5 + 8 = 13.

Example 2:

  • Input: root = [5, 3, 8, 2, 4, null, 6, 1]
Image
  • Expected Output: 1
  • Justification: The only left leaf node is 1 (left child of 2). So, the answer is 1.

Example 3:

  • Input: root = [1, null, 5, null, 4]
Image
  • Expected Output: 0
  • Justification: The tree doesn't have any left leaf node.

Constraints:

  • The number of nodes in the tree is in the range [1, 1000].
  • -1000 <= Node.val <= 1000

Solution

To solve this problem, we use Depth-First Search (DFS) to traverse the binary tree and find all left leaf nodes. Starting from the root, we recursively explore each node's left and right children. If a node is a leaf (it has no children) and is a left child (tracked using a boolean flag), we add its value to the total sum. For every node, we check its left child by setting the flag as true and its right child with the flag as false. This approach ensures that only left leaf nodes are considered in the sum, and we continue this process until all nodes are visited.

Step-by-Step Algorithm

Start from the root node:

  • Call dfs with the root node and isLeft set to false to indicate it is not a left child.

DFS() Function

  1. Check for a null node:

    • If the current node is null, return 0. This means there are no further nodes in this path.
  2. Identify a left leaf node:

    • If the current node is a leaf (node.left == null && node.right == null) and isLeft is true, return node.val. This adds the value of the left leaf node to the sum.
  3. Recursive traversal for children:

    • Call dfs recursively for the left child (node.left) with isLeft set to true to track it as a left child.
    • Call dfs recursively for the right child (node.right) with isLeft set to false.
  4. Calculate total sum:

    • Return the sum of the results from the left and right child calls to accumulate the total sum of all left leaves in the tree.

Algorithm Walkthrough

Input: root = [3, 5, 10, null, null, 8, 7]:

Image

Start with the root node:

  • The root node is 3. Call dfs(3, false).
  • node is not null, so proceed further.

DFS() Function

  1. Check if root node is a left leaf:

    • node 3 is not a leaf (it has left and right children). Move to the next steps.
  2. Recursive call for the left child of root (3):

    • Call dfs(5, true).
    • node 5 is not null, so proceed further.
    • node 5 is a leaf node (node.left == null && node.right == null) and isLeft is true.
    • Return 5 (its value) as it is a left leaf.
  3. Recursive call for the right child of root (3):

    • Call dfs(10, false).
    • node 10 is not null, so proceed further.
    • node 10 is not a leaf node (it has left and right children), so move to the next steps.
  4. Recursive call for the left child of node (10):

    • Call dfs(8, true).
    • node 8 is not null, so proceed further.
    • node 8 is a leaf node (node.left == null && node.right == null) and isLeft is true.
    • Return 8 (its value) as it is a left leaf.
  5. Recursive call for the right child of node (10):

    • Call dfs(7, false).
    • node 7 is not null, so proceed further.
    • node 7 is a leaf node, but isLeft is false.
    • Return 0 as it is not a left leaf.
  6. Combine results for node (10):

    • The result for node 10 is dfs(8, true) + dfs(7, false), which is 8 + 0 = 8.
    • Return 8.
  7. Combine results for root node (3):

    • The result for the root node 3 is dfs(5, true) + dfs(10, false), which is 5 + 8 = 13.
    • Return 13.
  8. Final result:

    • The total sum of left leaves in the tree [3,5,10,null,null,8,7] is 13.

Code

Python3
Python3

. . . .

Complexity Analysis

Time Complexity

  • The time complexity of the above solution is O(n), where n is the number of nodes in the binary tree.
  • This is because the algorithm visits each node exactly once in a Depth-First Search (DFS) manner to determine whether it is a left leaf.

Space Complexity

  • The space complexity of the solution is O(h), where h is the height of the binary tree.
  • This is due to the recursion stack used in the DFS. In the worst case, if the tree is completely unbalanced, the recursion stack would go up to O(n), but in a balanced binary tree, it would be O(log n).

.....

.....

.....

Like the course? Get enrolled and start learning!

Table of Contents

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible

Contents are not accessible