 All Problems
Sum Root to Leaf Numbers
medium
tree
depth-first search
math
binary tree
amazon
google
facebook

You are given a binary tree containing digits from 0 to 9 only. Each root-to-leaf path in the tree represents a number. Return the total sum of all root-to-leaf numbers.

Example 1:

Input:  1 2 3
Output: 25
Explanation: 12 + 13 = 25

Example 2:

Input:  4 9 0 5 1
Output: 1026
Explanation: 495 + 491 + 40 = 1026

Constraints:

  • The number of nodes is in the range [1, 1000]
  • 0 ≤ Node.val ≤ 9
  • The depth of the tree will not exceed 10

Input format: BFS level-order, space-separated, null for missing nodes.

Output format: Total sum.

Run to check your code against the sample cases, or submit to run every case