Deepest Leaves Sum

Given the `root` of a binary tree, return the sum of values of its deepest leaves.

30:00

Deepest Leaves Sum
medium
Free written solution
Topics
Companies

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
Constraints:
  • The number of nodes in the tree is in the range [1,104][1, 10^4].

  • 1≤Node.val≤1001 \leq \text{Node.val} \leq 100

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"root":[1,2,3,4,5,null,6,7,null,null,null,null,8]}

Start with level = [1]. Each pass of the loop sums one whole row, and the sum restarts at every new row.

????12345678

total

0

level

1

next

empty

node being added
row being summed
collected for the next row
sum thrown away
deepest row
Variables
VariableValue
row-
level1
next0
total0
DepthFunction Call
Stack empty
0/24