Deepest Leaves Sum (LeetCode 1302): Solution and Explanation
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
- Start with
level = [root]. - While
levelis not empty, settotal = 0and an empty listnext. - For every node in
level, add its value tototaland append its non-null children tonext. - Set
level = next. When the loop ends, returntotal.
Step-by-step example
root = [1,2,3,4,5,null,6,7,null,null,null,null,8]
- Row 0 is [1]. total = 1, next = [2, 3].
- Row 1 is [2, 3]. total restarts and becomes 5, next = [4, 5, 6].
- Row 2 is [4, 5, 6]. total becomes 15, next = [7, 8].
- Row 3 is [7, 8]. total becomes 15, and next is empty.
- 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 total1class Solution {
2 public int deepestLeavesSum(TreeNode root) {
3 List<TreeNode> level = new ArrayList<>();
4 level.add(root);
5 int total = 0;
6 while (!level.isEmpty()) {
7 total = 0;
8 List<TreeNode> next = new ArrayList<>();
9 for (TreeNode node : level) {
10 total += node.val;
11 if (node.left != null) next.add(node.left);
12 if (node.right != null) next.add(node.right);
13 }
14 level = next;
15 }
16 return total;
17 }
18}1class Solution {
2public:
3 int deepestLeavesSum(TreeNode* root) {
4 vector<TreeNode*> level{root};
5 int total = 0;
6 while (!level.empty()) {
7 total = 0;
8 vector<TreeNode*> next;
9 for (TreeNode* node : level) {
10 total += node->val;
11 if (node->left) next.push_back(node->left);
12 if (node->right) next.push_back(node->right);
13 }
14 level = next;
15 }
16 return total;
17 }
18};Time and space complexity
O(n)
Every node is added to a row list once and read once.
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.