Deepest Leaves Sum (LeetCode 1302): Solution and Explanation

Medium
Tree
Depth First Search
Breadth First Search
Binary Tree

Given a binary tree, return the sum of the values on its deepest row. Every node on that row is a leaf, since nothing lies below it.

Intuition

You do not know which row is the deepest until you have looked below every row. So any approach either finds the depth first or keeps a candidate answer and replaces it.

A breadth-first search that handles one complete row per loop iteration makes the replacing free. Set total to 0 at the start of each row and add up that row's values.

Each new row overwrites the previous row's sum. When no row is left, total holds the sum of the bottom row, which is exactly the deepest leaves.

Approach: Level-by-level BFS

  1. Start with level = [root].
  2. While level is not empty, set total = 0 and an empty list next.
  3. For every node in level, add its value to total and append its non-null children to next.
  4. Set level = next. When the loop ends, return total.

Step-by-step example

root = [1,2,3,4,5,null,6,7,null,null,null,null,8]
  1. Row 0 is [1]. total = 1, next = [2, 3].
  2. Row 1 is [2, 3]. total restarts and becomes 5, next = [4, 5, 6].
  3. Row 2 is [4, 5, 6]. total becomes 15, next = [7, 8].
  4. Row 3 is [7, 8]. total becomes 15, and next is empty.
  5. No row lies below, so return 15, the sum of 7 and 8.

Code: Python, Java, C++

1class Solution:
2    def deepestLeavesSum(self, root: Optional[TreeNode]) -> int:
3        level = [root]
4        while level:
5            total = 0
6            nxt = []
7            for node in level:
8                total += node.val
9                if node.left:
10                    nxt.append(node.left)
11                if node.right:
12                    nxt.append(node.right)
13            level = nxt
14        return total
Press play on the visualizer and the running line lights up here.

Time and space complexity

Time

O(n)

Every node is added to a row list once and read once.

Space

O(w)

Two row lists are alive at a time, so memory grows with the widest row w, which can be about n / 2.

Other approaches

DFS with depth tracking (time O(n), space O(h))

Carry the depth down. When a node is deeper than the best depth seen, reset the sum to its value. When it is equally deep, add to the sum.

Two passes (time O(n), space O(h))

First compute the tree's height, then walk again and add only the nodes at that depth.

Common mistakes

  • Summing every leaf. Leaves can sit at different depths, and only the bottom row counts.
  • Forgetting to reset the sum at each new row, which returns the sum of the whole tree.
  • Using one queue without separating rows, so the code cannot tell where one row ends and the next begins.

Frequently asked questions

Are the deepest nodes always leaves?

Yes. A node on the bottom row cannot have children, or those children would form a deeper row.

Can the sum overflow an int?

No. There are at most 10^4 nodes with values up to 100, so the sum is at most 10^6.

BFS or DFS?

Both are O(n) time. BFS memory follows the widest row and DFS memory follows the height, so pick based on the shape you expect.