Deepest Leaves Sum

MediumTreeslevel order

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

Given the root of a non-empty binary tree, return the sum of the values of all nodes that sit at the **greatest depth** in the tree. Nodes at the deepest level are necessarily leaves; leaves at shallower depths are not counted. A level-by-level BFS where the last level processed is the answer runs in O(n).

Examples

Input: root = [1,2,3,4,5,null,6,7,null,null,null,null,8]
Output: 15
The deepest level holds 7 and 8, so the sum is 15.
Input: root = [5]
Output: 5
The root is the only node and the deepest one.

Constraints

  • 1 <= number of nodes <= 10^4
  • 1 <= Node.val <= 100

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More level order problems