Sum Root to Leaf Numbers
Given the root of a binary tree where every node holds a single digit 0-9, each root-to-leaf path spells a number by reading digits from root to leaf. Return the total sum of all the numbers spelled by the root-to-leaf paths.
Open official problem prompt ↗Add up every number formed by reading digits along a root-to-leaf path.
Like reading an odometer as you drive down each branch: each turn deeper shifts the current reading left by one place and drops in the next digit; you record the reading only when you reach the end of a road.
- Input
- root = [1,2,3]
- Output
- 25
- Why
- Path 1->2 spells 12 and path 1->3 spells 13; 12 + 13 = 25.
The number of nodes is in the range [1, 1000]0 <= Node.val <= 9The depth of the tree will not exceed 10