Reported September 2026
ByteDancedepth first search

Binary Tree Target-Sum Paths

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

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

The edge case that sinks most first attempts at this ByteDance OA, reported in September 2026, is the empty tree with targetSum 0. It's a tree problem: find every root-to-leaf path that sums to targetSum, in left-to-right order. The idea is simple. The details are where people lose points. Negative values, a node that matches the sum but isn't a leaf, and the root being null all trip up quick solutions. If you blank during the live assessment, StealthCoder sits invisibly on your desktop as a safety net and gives you the solution in real time.

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 is a node with no left child and no right child. Each returned path lists its values from the root to that leaf.
Return qualifying paths in left-to-right depth-first order: completely visit a node's left subtree before its right subtree.

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]]
The two left-to-right root-to-leaf paths with sum 22 are 5 → 4 → 11 → 2 and 5 → 8 → 4 → 5.
Example 2
root = [1,2,3]
targetSum = 5
return = []
The two root-to-leaf sums are 3 and 4, so neither path qualifies.
Example 3
root = []
targetSum = 0
return = []
An empty tree has no root-to-leaf paths.

Constraints
The tree contains at most 5000 nodes.
Each node value is between -1000 and 1000, inclusive.
-10^9 <= targetSum <= 10^9.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick is depth-first search with backtracking. Keep one running path list and a remaining sum. At each node, append its value and subtract it from the remaining sum. If the node is a leaf (no left and no right child) and the remaining sum is exactly 0, copy the path into the results. Then recurse left before right, which gives the required order. Pop the value on the way back up. The pitfalls are concrete. First, you must copy the path when you save it, or later pops will wipe it out. Second, don't prune when the sum goes negative or exceeds the target, because values can be negative. Third, only count leaves, so a mid-tree node that hits the target doesn't qualify. Fourth, return an empty list for a null root. StealthCoder is the hedge if you freeze on the backtracking cleanup during the live OA.

StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.

If this hits your live OA

You can drill Binary Tree Target-Sum Paths 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. If you're reading this with an OA window open, you're who this was built for.

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 ByteDance's OA.

ByteDance reuses patterns across OAs. If you're reading this with an OA window open, you're who this was built for. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Binary Tree Target-Sum Paths FAQ

What's the trick to Binary Tree Target-Sum Paths?+

Run DFS with a shared path list and backtrack. Add the node value, check if it's a leaf with remaining sum equal to zero, recurse left then right, then pop. Copy the path when you record it, or later pops will corrupt your results.

Why can't I stop early when the running sum passes the target?+

Node values range from -1000 to 1000, so a sum that overshoots can come back down later. Pruning on sum would miss valid paths. You have to walk to every leaf, which is fine for at most 5000 nodes.

What edge cases should I test before submitting?+

Test an empty tree, which returns an empty list even when targetSum is 0. Test a single node that equals the target. Test a non-leaf node whose prefix hits the target, which must not count. Test negative values and a target that's negative.

How hard is this really?+

It's a standard medium-level tree DFS. The idea is short, but the bugs hide in path copying, leaf checks and null roots. If you've written a backtracking recursion before, you can finish it quickly and spend the rest on edge cases.

How do I prepare in 48 hours?+

Write this one from scratch twice, recursively, without peeking. Then do one variant where you return only a boolean for any path sum. Focus on the append, recurse, pop rhythm and the leaf condition. That covers most tree-path questions.

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

OA at ByteDance?
Invisible during screen share
Get it