Sum Root to Leaf Numbers
Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.
Practice this as a mock interviewFree to start, no account neededProblem
Every node in the binary tree rooted at root holds a single digit from 0 to 9. Reading the digits along a root-to-leaf path, from root to leaf, forms a decimal number. For instance, the path 1 -> 2 -> 3 forms 123. Leading zeros are allowed and simply do not add value (path 0 -> 4 is 4).
Return the sum of the numbers formed by **all** root-to-leaf paths. A leaf is a node with no children.
The tree has at most 10 levels, so a single path forms a number of at most 10 digits. Return the total as a 64-bit integer.
Examples
Constraints
- 1 <= number of nodes <= 1000
- 0 <= Node.val <= 9
- The depth of the tree is at most 10
What the interviewer grades
A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.