FastPrepSum of Numbers Along Root-to-Leaf Paths
Problems/Sum of Numbers Along Root-to-Leaf Paths

Sum of Numbers Along Root-to-Leaf Paths

Adobe logoAdobe● MediumFULLTIMEPHONE SCREEN

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: You add up the numbers that the digit paths of a binary tree spell. A March 2022 post is by an MTS-2 hire with about three years at a startup, who got an offer. It links this classic task as the only DSA item of the first of three hour-long rounds. The rest of that round went to the author's resume projects and Java topics. The post gives only the link.

The rules are those of the linked task, with one change. The sum is returned as a 64-bit integer, with no promise that it fits in 32 bits. We chose these limits: 1 to 1000 nodes and at most 10 levels. The answer is 64-bit. We wrote new examples.

Our version is about 90% the same as the reported question.

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) → long

Examples

Example 1

root = [7,3,0,null,8,4]return = 1442
root
73804

The 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 = 504
root
2513

The 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 = 6
root
06

The only leaf is 6. The path 0, 6 spells 06, which is the number 6.

Constraints

  • The tree has 1 to 1000 nodes.
  • 0 <= Node.val <= 9
  • The tree has at most 10 levels.

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 MTS Interview Questions: 12 Experiences (Part 2 of 6)

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)
All Adobe interview questionsSee Adobe hiring insights
Saved
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public long sumPathNumbers(TreeNode root) {
        // Write your code here.
    }
}
Checking account…