Sum Root-to-Leaf Numbers
Updated
Problem statement
You are given the root of a binary tree. Every node stores one decimal digit from 0 through 9. Each root-to-leaf path represents the number formed by reading its digits from root to leaf.
Return the sum of the numbers represented by all root-to-leaf paths. A leaf has no children. Return 0 for an empty tree.
Follow-up
How can an iterative traversal avoid call-stack overflow on a very deep tree?
Function
sumNumbers(root: TreeNode) → intExamples
Example 1
root = [1,2,3]return = 25The paths form 12 and 13, whose sum is 25.
Example 2
root = [4,9,0,5,1]return = 1026The paths form 495, 491, and 40.
Example 3
root = []return = 0An empty tree has no root-to-leaf paths.
Constraints
- Each node value is between
0and9. - The tree may be empty.
- The final sum fits in a signed
32-bit integer.
More Meta Onsite Interview questions
- Multi-Head Attention Forward PassPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Run-Length Encoded Vector Dot ProductPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Mutual-Friend RecommendationsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Deterministic K-Means AssignmentsPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
- Linear Layer Forward and Backward PassesONSITE INTERVIEW · Seen Sep 2026
- Reservoir Sampling with Recorded DrawsONSITE INTERVIEW · Seen Sep 2026
- Stochastic Gradient Descent with MomentumONSITE INTERVIEW · Seen Sep 2026
- Top K Largest Values with a HeapONSITE INTERVIEW · Seen Sep 2026
- Partition a Card Deck into FifteensONSITE INTERVIEW · Seen Sep 2026
- Insert into a Sorted Circular ListONSITE INTERVIEW · Seen Aug 2026
Similar questions at other companies
- Maximum Root-to-Leaf and Leaf-to-Leaf Path SumsAmazon · ONSITE INTERVIEW
- Binary Tree Level Order TraversalGoogle · ONSITE INTERVIEW
- Lowest Common Ancestor Only When Both Targets ExistAdobe · ONSITE INTERVIEW
- Validate Binary Search TreeGoldman Sachs · ONSITE INTERVIEW
- Balanced Tree Node ReportNetflix · ONSITE INTERVIEW
- Trim a Binary Search TreeBloomberg LP · ONSITE INTERVIEW
- Height-Balanced Binary TreeExpedia · ONSITE INTERVIEW
- Binary Tree Target-Sum PathsByteDance · PHONE SCREEN
- Validate Binary Search TreeLyft · PHONE SCREEN
- Lowest Common Ancestor in a Binary TreeZillow · PHONE SCREEN