72. Path Sum II
mediumAsked at VercelReturn all root-to-leaf paths where the sum equals target. Vercel asks this for the backtracking-with-running-sum pattern — same shape as their cost-bounded route discovery in the deployment graph.
By Alex Chen, Founder, InterviewChamp.AI · Last verified
Source citations
Public interview reports confirming this problem appears in Vercel loops.
- Glassdoor (2025-12)— Vercel platform onsite; backtracking expected.
- Blind (2026-Q1)— Listed in Vercel screen pool.
Problem
Given the root of a binary tree and an integer targetSum, return all root-to-leaf paths where the sum of the node values in the path equals targetSum. Each path should be returned as a list of the node values, not node references. A root-to-leaf path is a path starting from the root and ending at any leaf node.
Constraints
The number of nodes in the tree is in the range [0, 5000].-1000 <= Node.val <= 1000-1000 <= targetSum <= 1000
Examples
Example 1
root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22[[5,4,11,2],[5,8,4,5]]Example 2
root = [1,2,3], targetSum = 5[]Approaches
1. DFS collecting all root-to-leaf paths, filter
Collect every path; keep those with sum == target.
- Time
- O(n * h)
- Space
- O(n)
// O(n) paths in the worst case, each O(h). Better to filter on the fly.Tradeoff: Wastes work emitting non-matching paths.
2. Backtracking with running remaining (optimal)
Track remaining = target - sum-so-far. At leaves, push path if remaining == 0. Use mutable path with push/pop for O(h) extra storage per path.
- Time
- O(n^2) worst-case output
- Space
- O(h)
function pathSum(root, targetSum) {
const out = [];
const path = [];
function dfs(node, remaining) {
if (!node) return;
path.push(node.val);
if (!node.left && !node.right && remaining === node.val) {
out.push([...path]);
}
dfs(node.left, remaining - node.val);
dfs(node.right, remaining - node.val);
path.pop();
}
dfs(root, targetSum);
return out;
}Tradeoff: The mutable path with push/pop is the canonical backtracking pattern. Copying via spread on emit is the only allocation beyond the result itself.
Vercel-specific tips
Vercel grades the backtracking with mutable path. Bonus signal: emphasizing the leaf check (BOTH children null) rather than 'no left subtree' alone. Also flag that values can be negative — early-exit pruning based on remaining doesn't work.
Common mistakes
- Pushing path WITHOUT spread on emit — collects a reference that mutates after backtrack.
- Defining 'leaf' as 'no left child' — wrong, must be both null.
- Trying to prune on remaining < 0 — fails when negative values exist.
Follow-up questions
An interviewer at Vercel may pivot to one of these next:
- Path Sum (LC 112) — only boolean answer.
- Path Sum III (LC 437) — paths anywhere in the tree.
- Binary Tree Paths (LC 257) — all root-to-leaf paths without the sum constraint.
Solve it now
Free. No sign-up. Python and JavaScript run instantly in your browser.
FAQ
Why push then pop?
The path array is shared across all recursive calls. Push before recursion, pop after — this restores the array for the sibling subtree, exactly the backtracking idiom.
Why spread on emit?
If you push the same array reference, the post-emit pops would corrupt the saved path. Spreading captures a snapshot.
Practice these live with InterviewChamp.AI
Drill Path Sum II and other Vercel interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.
Practice these live with InterviewChamp.AI →