Reported September 2026
Amazondepth first search

Root-to-Leaf Paths with a Target Sum

Reported by candidates from Amazon's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Amazon OA. Under 2s to a working solution.
Founder's read

The whole problem lives in one data structure: a binary tree, and you walk it depth-first. Amazon reported this root-to-leaf target sum question in September 2026, and it's a classic path-collection problem dressed up as an OA. You get a root and a targetSum, and you return every root-to-leaf path that adds up to it, in left-to-right order. If you've seen DFS with backtracking, this is a ten-minute job. If you blank under the timer, StealthCoder runs invisibly on your screen and hands you a working solution as a safety net. Here's the pattern and the traps.

The problem

Given the root of a binary tree and an integer targetSum, return every root-to-leaf path whose node values add up to targetSum.
A leaf has no children. Return paths in left-to-right depth-first order, with each path listed from the root to its leaf.

Function
pathSum(root: TreeNode, targetSum: int) → int[][]

Examples
Example 1
root = [5,4,8,11,null,13,4,7,2,null,null,5,1]
targetSum = 22
return = [[5,4,11,2],[5,8,4,5]]
Both listed root-to-leaf paths sum to 22, and the left path is visited first.
Example 2
root = [1,2,3]
targetSum = 5
return = []
Neither root-to-leaf path sums to 5.
Example 3
root = []
targetSum = 0
return = []
An empty tree has no root-to-leaf path.

Constraints
The tree contains between 0 and 500 nodes.
-1000 ≤ Node.val ≤ 1000.
-10^9 ≤ targetSum ≤ 10^9.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick is DFS with a running path and a remaining sum. At each node, push its value onto the path and subtract it from the target. If the node is a leaf (no left and no right child) and the remaining sum is zero, copy the path into the results. Then recurse left, then right, and pop the value on the way out. That pop is the backtracking step. Pitfalls are concrete. You must copy the path before storing it, or every entry mutates later. Check for a leaf, not just null, because an interior node with one child isn't a path end. Don't prune when the remaining sum goes negative, since node values can be negative. An empty tree returns an empty list. Visiting left before right gives the required order for free. If the recursion details slip during the live OA, StealthCoder is the hedge that gets you unstuck.

Memorize the pattern. If you can't, run StealthCoder. The proctor sees the IDE. They don't see what's behind it.

If this hits your live OA

You can drill Root-to-Leaf Paths with a Target Sum cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge.

Get StealthCoder

Related leaked OAs

⏵ Practice the LeetCode equivalent

This OA pattern shows up on LeetCode as path sum ii. If you have time before the OA, drill that.

⏵ The honest play

You've seen the question. Make sure you actually pass Amazon's OA.

Amazon reuses patterns across OAs. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Root-to-Leaf Paths with a Target Sum FAQ

How hard is this Amazon root-to-leaf path sum question really?+

It's a medium at most. The pattern is standard DFS with backtracking on a binary tree. The only real work is tracking the path, checking for true leaves, and copying the path when you record it. Most candidates who know recursion finish it quickly.

What's the trick to getting this right?+

Carry a mutable path list and a remaining sum down the recursion. At a leaf with remaining sum zero, store a copy of the path. After both child calls, pop the current node. That push, recurse, pop cycle is the whole solution.

Can I stop early when the running sum exceeds the target?+

No. Node values can be negative, from -1000 to 1000, so a sum that overshoots can still come back down. You have to explore every root-to-leaf path. With at most 500 nodes, full traversal is fast enough.

What edge cases break most solutions?+

An empty tree must return an empty list, even with targetSum 0. A node with one child isn't a leaf, so don't count it as a path end. Also copy the path when saving it, or later pops will corrupt your stored results.

How do I prepare for this in 48 hours?+

Write the recursive DFS with backtracking from scratch twice on a small tree. Trace Example 1 by hand and confirm you get [[5,4,11,2],[5,8,4,5]]. Then test the empty tree and a negative-value case. That covers nearly everything this problem can throw at you.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Amazon.

OA at Amazon?
Invisible during screen share
Get it