FrontendX
Sum Root to Leaf Numbers
medium
Description
AI Assistance
Solution
Test Cases
Test Result
Submissions
Canvas
Sum Root to
Leaf Numbers
Each root-to-leaf path forms a number by concatenating digits. Return the sum of all such numbers.
Examples
Example 1
Input:
[1,2,3]
Output:
25
Example 2
Input:
[4,9,0,5,1]
Output:
1026
Hints
Hint 1
Use recursion to traverse the tree, passing the current path's number as an argument to child nodes.
Hint 2
When a leaf node is reached, return the current number; otherwise, return the sum of numbers from left and right subtrees.
Hint 3
Handle edge cases where the tree is empty or contains only one node (root is a leaf).
Related Problems
maximum-depth-of-binary-tree
same-tree
invert-binary-tree
Sum Root to Leaf Numbers
Each root-to-leaf path forms a number by concatenating digits. Return the sum of all such numbers.