You are given the root of a binary tree containing digits from 0 to 9 only. Each root-to-leaf path in the tree represents a number. - For example, the root-to-leaf path 1 -> 2 -> 3 represents the number 123. Return the total sum of all root-to-leaf numbers. Test cases are generated so that the answer will fit in a 32-bit integer. A leaf node is a node with no children.
Example 1
Input: root = [4,9,0,5,1]
Output: 1026
Explanation: The root-to-leaf paths spell 4-9-5 = 495, 4-9-1 = 491 and 4-0 = 40. Their sum is 495 + 491 + 40 = 1026.
Example 2
Input: root = [1,2,3]
Output: 25
Explanation: The two paths spell 12 and 13, and 12 + 13 = 25.
Example 3
Input: root = [1]
Output: 1
Explanation: The single node is a leaf, so the only number is 1.
Carry the number formed so far down each path. At each node the running number becomes cur*10 + node.val. When you reach a leaf, that running value is a complete root-to-leaf number; sum those over all leaves.
Run checks all cases above. Submit evaluates all test cases.