Deepest Leaves Sum
MEDIUMDescription
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, 104]. 1 <= Node.val <= 100
Approaches
Checkout 3 different approaches to solve Deepest Leaves Sum. Click on different approaches to view the approach and algorithm in detail.
Two-Pass Depth-First Search (DFS)
This approach involves two separate traversals of the tree. The first traversal is to determine the maximum depth of the tree. The second traversal then sums up the values of all nodes that are at this maximum depth.
Algorithm
- First Pass: Find Maximum Depth
- Define a recursive function
findMaxDepth(node). - The base case is a null node, which has a depth of 0.
- For a non-null node, the depth is
1 + max(findMaxDepth(node.left), findMaxDepth(node.right)). - Call this on the
rootto find themaxDepthof the entire tree.
- Define a recursive function
- Second Pass: Sum at Maximum Depth
- Define a second recursive function
sumAtDepth(node, currentDepth, maxDepth). - Initialize a sum variable to 0.
- Traverse the tree, passing the
currentDepth. - If
currentDepthequalsmaxDepth, add the node's value to the sum. - Recursively call for left and right children with
currentDepth + 1.
- Define a second recursive function
- Return the final sum.
This method breaks the problem down into two simpler, sequential steps.
-
First Pass - Find Maximum Depth: We first need to know what the 'deepest' level is. We can find this by performing a standard DFS traversal. A recursive function
findMaxDepth(node)can compute the height of the subtree rooted atnode. The height of a null node is 0, and the height of a non-null node is 1 plus the maximum height of its left and right subtrees. By calling this on the root, we get the maximum depth of the entire tree. -
Second Pass - Sum Deepest Leaves: Once we have the
maxDepth, we traverse the tree again. This time, we use another recursive function, saysumAtDepth(node, currentDepth, maxDepth). We pass down the current depth of each node. If a node'scurrentDepthmatches themaxDepthwe found earlier, we add its value to a running total. After this second traversal is complete, the total will be our answer.
class Solution {
int sum = 0;
public int deepestLeavesSum(TreeNode root) {
if (root == null) {
return 0;
}
int maxDepth = findMaxDepth(root);
sumAtDepth(root, 1, maxDepth);
return sum;
}
private int findMaxDepth(TreeNode node) {
if (node == null) {
return 0;
}
return 1 + Math.max(findMaxDepth(node.left), findMaxDepth(node.right));
}
private void sumAtDepth(TreeNode node, int currentDepth, int maxDepth) {
if (node == null) {
return;
}
if (currentDepth == maxDepth) {
sum += node.val;
}
sumAtDepth(node.left, currentDepth + 1, maxDepth);
sumAtDepth(node.right, currentDepth + 1, maxDepth);
}
}
Complexity Analysis
Pros and Cons
- Conceptually straightforward as it breaks the problem into two distinct, simpler subproblems.
- Inefficient as it requires traversing the entire tree twice.
Code Solutions
Checking out 3 solutions in different languages for Deepest Leaves Sum. Click on different languages to view the code.
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeNode left, TreeNode right) { * this.val = val; * this.left = left; * this.right = right; * } * } */ class Solution { public int deepestLeavesSum ( TreeNode root ) { Deque < TreeNode > q = new ArrayDeque <>(); q . offer ( root ); int ans = 0 ; while (! q . isEmpty ()) { ans = 0 ; for ( int n = q . size (); n > 0 ; -- n ) { root = q . pollFirst (); ans += root . val ; if ( root . left != null ) { q . offer ( root . left ); } if ( root . right != null ) { q . offer ( root . right ); } } } return ans ; } }Video Solution
Watch the video walkthrough for Deepest Leaves Sum
Similar Questions
5 related questions you might find useful
Algorithms:
Data Structures:
Companies:
Subscribe to Scale Engineer newsletter
Learn about System Design, Software Engineering, and interview experiences every week.
No spam, unsubscribe at any time.