Skip to main content

Deepest Leaves Sum

LeetCode 1254 | Difficulty: Medium​

Medium

Problem 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.

When to use

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.

When to use

Level-order traversal, level-based aggregation, right/left side view.


Solutions​

Solution 1: C# (Best: 152 ms)​

MetricValue
Runtime152 ms
Memory44.4 MB
Date2022-01-06
Solution
/**
* 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​

ApproachTimeSpace
Tree Traversal$O(n)$$O(h)$

Interview Tips​

Key Points
  • 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.