Sum of Numbers Along Root-to-Leaf Paths
Updated
Problem statement
Each node of a binary tree holds one digit. The digits along a path from the root down to a leaf, read from top to bottom, spell a number: for example, the path 4, 0, 6 spells 406. A leaf is a node with no children.
Given the root of the tree, return the sum of the numbers spelled by all root-to-leaf paths. Leading zeros are allowed (the path 0, 6 spells 6). The sum can be larger than a 32-bit integer can hold, so return it as a 64-bit integer.
Function
sumPathNumbers(root: TreeNode) → longExamples
Example 1
root = [7,3,0,null,8,4]return = 1442The leaves are 8 and 4. The path 7, 3, 8 spells 738 and the path 7, 0, 4 spells 704. The sum is 738 + 704 = 1442.
Example 2
root = [2,null,5,1,3]return = 504The root 2 has no left child, but it is not a leaf, because it has a right child. The paths 2, 5, 1 and 2, 5, 3 spell 251 and 253, so the sum is 504.
Example 3
root = [0,6]return = 6The only leaf is 6. The path 0, 6 spells 06, which is the number 6.
Constraints
- The tree has
1to1000nodes. 0 <= Node.val <= 9- The tree has at most
10levels.
More Adobe Phone Screen questions
- Evaluate a Parenthesized Integer ExpressionPHONE SCREEN · Seen Oct 2026
- All Elements of Two BSTs in OrderPHONE SCREEN · Seen Aug 2026
- Rotate an Array In PlaceOA · PHONE SCREEN · Seen Aug 2026
- Binary Tree Level-Order Traversal by LevelsPHONE SCREEN · ONSITE INTERVIEW · Seen Aug 2026
- Count Occurrences in a Sorted ArrayPHONE SCREEN · ONSITE INTERVIEW · Seen Jul 2026
- Count Pairs with Absolute Difference KPHONE SCREEN · Seen Jul 2026
- Count Subsets with Sum KPHONE SCREEN · Seen Jul 2026
- Numbers with Exactly Three DivisorsPHONE SCREEN · Seen Jul 2026
- Sum of Even Nodes on a Path in a Numbered Binary TreePHONE SCREEN · Seen Jul 2026
- Largest Value in Every Sliding WindowPHONE SCREEN · Seen Jul 2026
Similar questions at other companies
- N-ary Index-Path Encoder and DecoderBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
- House Robber IIIOkta · PHONE SCREENOkta interview questions
- Render a Task Tree from CSV RowsStripe · PHONE SCREEN · ONSITE INTERVIEWStripe interview questions
- Validate Binary Search TreeKotak Mahindra Bank · PHONE SCREENKotak Mahindra Bank interview questions
- House Robber IIIGoldman Sachs · PHONE SCREENGoldman Sachs interview questions
- Binary Tree Target-Sum PathsByteDance · PHONE SCREENByteDance interview questions
- House Robber IIIVisa · PHONE SCREENVisa interview questions
- Find All Paths for a Name in a File SystemThe Walt Disney Company · PHONE SCREENThe Walt Disney Company interview questions
- Validate a BST with a Custom ComparisonApple · PHONE SCREENApple interview questions
- Filter Undesired Comments and Their DescendantsReddit · PHONE SCREEN · ONSITE INTERVIEWReddit interview questions
Interview reports that mention this question
Adobe interview experiences
- Adobe Web, QA, DevOps, ML Roles: 13 Experiences (Part 3 of 3)
- Adobe SWE Interviews, Level Unknown: 25 Experiences (Part 2 of 3)
- Adobe Interviews, Level Unknown: 38 Experiences (Part 1 of 3)
- Adobe New Grad MTS Interview Questions: 7 Experiences
- Adobe ML/Research Intern Interviews: 12 Experiences (Part 4 of 4)