Deepest Leaves Sum Problem
Description
LeetCode Problem 1302.
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
Solution
Approach: Level-Order Traversal
The deepest leaves are exactly the nodes on the last level of the tree: any node on the last level has no children, and any leaf above it is not the deepest. So we only need the sum of the last level.
Walk the tree level by level (BFS). For each level, collect the children of its nodes. When a level has no children at all, it is the last one, and the answer is the sum of its values. (The Java and C++ versions do the same with a queue: they recompute the sum of every level and keep the last one.)
Complexity: O(n) time, since every node is visited once, and O(w) extra space, where w is the largest number of nodes on one level.
class Solution:
def deepestLeavesSum(self, root: Optional[TreeNode]) -> int:
level = [root]
while True:
below = [child for node in level for child in (node.left, node.right) if child]
if not below:
# Nothing below this level, so its nodes are the deepest leaves.
return sum(node.val for node in level)
level = below