Sum Root to Leaf Numbers
A medium Binary Trees interview guide. Task statement, worked examples, intuition, and step-by-step solutions.
- Topic
- Binary Trees
- Sheets
- 1
- Core for
- 4 roles
The problem
Given the root of a binary tree where each node contains a single digit from 0 to 9, return the total sum of all root-to-leaf numbers. Each root-to-leaf path represents a number formed by concatenating the digits.
Example 1
- Input
- [2,3,4,5,6,null,7]
- Output
- 718
- Why
- Paths: 2->3->5=235, 2->3->6=236, 2->4->7=247. Sum = 235+236+247 = 718.
Example 2
- Input
- [4,9,0,5,1,null,null,null,null,null,8]
- Output
- 1026
- Why
- Paths: 4->9->5=495, 4->9->1=491, 4->0=40. Sum = 495 + 491 + 40 = 1026.
Example 3
- Input
- [0]
- Output
- 0
- Why
- A single node with value 0 represents the number 0.
Constraints
- The number of nodes is in the range [1, 1000].
- 0 <= Node.val <= 9
How to think about it
Updated 2026-09-29Appending a decimal digit to a number shifts earlier digits one place to the left, which is `currentNumber * 10 + node.val`. Carry this running integer down each path. When landing on a leaf, return the assembled integer; for internal nodes, sum the totals returned by both child branches.
Approaches, worst first
Path string accumulation
time O(n * h) · space O(h)
Accumulate digit characters into strings during DFS. When a leaf is reached, parse the string into an integer and add to a running total. Incurs string allocation and parsing overhead at each step.
Arithmetic DFS accumulatorWrite this one
time O(n) · space O(h)
Pass running integer `val = val * 10 + node.val` down recursive calls. If the node is a leaf, return `val`. Otherwise return `dfs(node.left, val) + dfs(node.right, val)`. Avoids all heap allocations and strings.
Where people lose marks · 3
- Adding the running value at non-leaf nodes causes intermediate path prefixes to be counted in the total sum.
- Missing the single-node tree case where root is itself a leaf; it must return its own value directly.
- In languages without arbitrary-precision integers, deep trees could cause 32-bit integer overflow; within problem constraints (depth <= 1000), keep calculations safe.
The theory behind it
Binary Trees — the ground this problem stands on. All Binary Trees problems
What Binary Trees is
A binary tree is a branching data structure that starts at a single top node called the root, like an upside-down family tree. Every node holds a piece of data and can branch out to at most two children below it, known as the left child and the right child. Because there is no ordering rule about which values go left or right, finding a specific item can require checking every single node in the entire tree.
When to reach for it
Reach for binary trees when problems present hierarchical data with left and right child pointers. Questions asking for tree height, maximum depth, path sums from root to leaf, diameter, lowest common ancestor, or checking whether two trees are mirror reflections of each other all signal binary tree traversals. Any problem asking to inspect or reconstruct a tree layer by layer or path by path belongs here.
How the pattern works
Think recursively by focusing on what a single node must do. If the current node is null, return the base answer immediately. Otherwise, ask the left child for its result, ask the right child for its result, and combine both answers with the current node value before returning up to the parent. For horizontal scans, use a queue to read nodes layer by layer, measuring the queue length at the start of each layer to group nodes by depth.
More Binary Trees problems
Target Roles
Core requirement for 4 roles:
Track in your role's order
Pick your target role and all 370 problems resequence to what that interview actually asks.
Start freeProblem set and role mapping as of .