root holds a single digit. Each path from the root down to a leaf spells out a number, read top to bottom. For example, the path 5 → 0 → 4 is the number 504.
Return the sum of the numbers spelled by all such paths. A leaf is a node with no children.
The answer is guaranteed to fit in a 32-bit integer.root=tree_node([3, 1, 8]) → 69
Two branches: 3 → 1 reads as 31 and 3 → 8 reads as 38, so the total is 31 + 38 = 69.
root=tree_node([2, 0, None, 7, 5]) → 412
The root has only a left child 0, which has two leaves. The numbers 207 and 205 add up to 412.
root=tree_node([6]) → 6
The root is itself a leaf, so the only number is 6.
Node.val ≤ 9root=tree_node([3, 1, 8])
69
Run checks the first 3 tests in your browser. Submit checks all 87 tests on the server, hidden ones included.