Problem 410
Deepest Leaves Sum
Medium · 20 pts · Tree, Breadth-First Search, Binary Tree
Problem
Find the sum of the values of the leaves that are deepest in the tree, that is, the nodes on the last level. Leaves on higher levels are not included, even though they are leaves too. A breadth-first traversal can simply keep the sum of the most recent level it processed.
Input
The first line contains N, the number of tokens. The second line contains N tokens describing the tree in level order (breadth-first, left to right), where 'null' marks a missing child, as on LeetCode.
Output
Print the sum of the deepest leaves.
Constraints
1 <= number of nodes <= 10000; 1 <= value <= 100
Example input
13 1 2 3 4 5 null 6 7 null null null null 8
Example output
15
Explanation
In the tree 1 / 2 3 / 4 5 _ 6 / 7 _ _ _ _ 8, the deepest level holds 7 and 8, so the answer is 15.