Practice Deepest Leaves Sum
Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.
Problem Statement
Add the values of the leaves at the greatest depth in the binary tree rooted at root. Leaves on shallower levels do not contribute. Return that sum. Practice approach: Traverse breadth-first and reset the level sum before processing each layer. After the final layer, its sum is the answer because all nodes at maximum depth are leaves. Alternatively, track the deepest depth and sum in a depth-first traversal. Both visit n nodes in O(n) time; breadth-first traversal uses O(w) queue space for maximum width w. Test a single-node tree and an uneven tree.
Source: Deepest Leaves Sum on LeetCode. Use the original task for its coding environment and submissions.
Examples
# Example 1
root = [1,2,3,4,5,null,6,7,null,null,null,null,8]
15
# Example 2
root = [6,7,8,2,7,1,3,9,null,1,4,null,null,null,5]
19
Constraints
Related LeetCode Problems
Practice more problems similar to Deepest Leaves Sum.
Review your practice with PhantomCodeAI
After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.