Sum Root to Leaf Numbers

Medium

Topics
TreeDFS

Each root-to-leaf path in a binary tree represents a number formed by concatenating the node digits (each node value is a single digit 0-9). Return the total sum of all root-to-leaf numbers.

Example 1

Input:  root = [1,2,3]
Output: 25
Explanation: Paths 12 and 13; 12 + 13 = 25.

Example 2

Input:  root = [4,9,0,5,1]
Output: 1026

Constraints

  • 1 <= number of nodes <= 1000
  • 0 <= Node.val <= 9
Run ⌘' · Submit ⌘⏎