Deepest Leaves Sum
LeetCode 1254 | Difficulty: Mediumβ
MediumProblem Descriptionβ
Given the root of a binary tree, return the sum of values of its deepest leaves.
Example 1:

Input: root = [1,2,3,4,5,null,6,7,null,null,null,null,8]
Output: 15
Example 2:
Input: root = [6,7,8,2,7,1,3,9,null,1,4,null,null,null,5]
Output: 19
Constraints:
- The number of nodes in the tree is in the range `[1, 10^4]`.
- `1 <= Node.val <= 100`
Topics: Tree, Depth-First Search, Breadth-First Search, Binary Tree
Approachβ
Tree DFSβ
Traverse the tree recursively (or with a stack). At each node, decide: what information do I need from the left/right subtrees? Process: go left β go right β combine results. Consider preorder, inorder, or postorder traversal based on when you need to process the node.
Path problems, subtree properties, tree structure manipulation.
Tree BFS (Level-Order)β
Use a queue to process the tree level by level. At each level, process all nodes in the queue, then add their children. Track the level size to know when one level ends and the next begins.
Level-order traversal, level-based aggregation, right/left side view.
Solutionsβ
Solution 1: C# (Best: 152 ms)β
| Metric | Value |
|---|---|
| Runtime | 152 ms |
| Memory | 44.4 MB |
| Date | 2022-01-06 |
/**
* Definition for a binary tree node.
* public class TreeNode {
* public int val;
* public TreeNode left;
* public TreeNode right;
* public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
public class Solution {
public int DeepestLeavesSum(TreeNode root) {
int sum = 0;
Queue<TreeNode> nodes = new Queue<TreeNode>();
nodes.Enqueue(root);
while(nodes.Count != 0)
{
sum = 0;
int count = nodes.Count;
for (int i = 0; i < count; i++)
{
var first = nodes.Dequeue();
if(first.left == null && first.right == null)
{
sum += first.val;
}
if (first.left != null) nodes.Enqueue(first.left);
if (first.right != null) nodes.Enqueue(first.right);
}
}
return sum;
}
}
π 2 more C# submission(s)
Submission (2022-01-05) β 249 ms, 44.1 MBβ
/**
* Definition for a binary tree node.
* public class TreeNode {
* public int val;
* public TreeNode left;
* public TreeNode right;
* public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
public class Solution {
public int DeepestLeavesSum(TreeNode root) {
var levelSums = new Dictionary<int, int>();
Queue<TreeNode> nodes = new Queue<TreeNode>();
int level = 0;
nodes.Enqueue(root);
while(nodes.Count != 0)
{
levelSums.Add(level, 0);
int count = nodes.Count;
for (int i = 0; i < count; i++)
{
var first = nodes.Dequeue();
if(first.left == null && first.right == null)
{
levelSums[level] += first.val;
}
if (first.left != null) nodes.Enqueue(first.left);
if (first.right != null) nodes.Enqueue(first.right);
}
level++;
}
return levelSums[level-1];
}
}
Submission (2022-01-05) β 269 ms, 44.5 MBβ
/**
* Definition for a binary tree node.
* public class TreeNode {
* public int val;
* public TreeNode left;
* public TreeNode right;
* public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
public class Solution {
public int DeepestLeavesSum(TreeNode root) {
int sum = 0;
Queue<TreeNode> nodes = new Queue<TreeNode>();
int level = 0;
nodes.Enqueue(root);
while(nodes.Count != 0)
{
sum = 0;
int count = nodes.Count;
for (int i = 0; i < count; i++)
{
var first = nodes.Dequeue();
if(first.left == null && first.right == null)
{
sum += first.val;
}
if (first.left != null) nodes.Enqueue(first.left);
if (first.right != null) nodes.Enqueue(first.right);
}
level++;
}
return sum;
}
}
Complexity Analysisβ
| Approach | Time | Space |
|---|---|---|
| Tree Traversal | $O(n)$ | $O(h)$ |
Interview Tipsβ
- Discuss the brute force approach first, then optimize. Explain your thought process.
- Consider: "What information do I need from each subtree?" β this defines your recursive return value.
- LeetCode provides 2 hint(s) for this problem β try solving without them first.
π‘ Hints
Hint 1: Traverse the tree to find the max depth.
Hint 2: Traverse the tree again to compute the sum required.