Path Sum II
Path Sum II is LeetCode 113 (Medium). Given the root of a binary tree and an integer targetSum, return every root-to-leaf path whose node values add up to targetSum, each as a list of values.
- A leaf is a node with no children. A path must start at the root and end at a leaf.
- Values and the target can be negative, so a running sum that overshoots can still come back.
- The tree may be empty, and it can be a 5,000-node chain.
- The number of nodes in the tree is in the range [0, 5000].
- -1000 <= Node.val <= 1000
- -1000 <= targetSum <= 1000
Intuition
Walk the tree depth-first with one shared path list: push a node when you step into it and pop it when you leave. At every moment the list holds exactly the values from the root down to the current node, so reaching a leaf with nothing left to find means the list is an answer.
- Remaining target: subtract each value on the way down, so the test at a leaf is a single comparison with 0.
- Copy on save: the list keeps changing after a match, so the answer stores a snapshot of it.
- Pop on the way out: removing the node gives the parent back its own path for the next branch.
"Return every path" in a tree is DFS with backtracking: one list grows as you go down, shrinks as you come back, and is copied whenever it forms a full answer.
Approach
Before reading on, decide what state each recursive call needs, what exactly makes a path count, and what you must undo before returning to the parent. Aim for one visit per node.
Step into a node
Append node.val to path and subtract it from remaining. Because remaining is a plain number passed into each call, every call has its own copy, and the parent's value is unchanged when the call returns.
Save a copy at a matching leaf
If the node has no children and remaining == 0, the list is a full root-to-leaf path with the right total. Append path[:] (a new list), not path, to the result.
Go left, then right
Recurse into each child that exists, left first. Leaves are then reached in left-to-right order, so the saved paths come out in the same order as the examples.
Pop before returning
After both children are done, remove the last value with path.pop(), whether or not anything matched. The list is then exactly what the parent had, so its other branch starts from the right prefix.
Code in Python, C++ and Java
Common pitfalls
Saving the list instead of a copy
res.append(path)
res.append(path[:])
Every saved entry points to the same list, which is popped back to empty by the end. For [1, 2] with target 3 the result becomes [[]] instead of [[1, 2]].
Treating a one-child node as a leaf
if (not node.left or not node.right) and remaining == 0:
if not node.left and not node.right and remaining == 0:
For [1, 2] with target 1, the root has only a left child, so or saves [1]. The only root-to-leaf path is 1 → 2, which sums to 3, so the answer is [].
Stopping once the sum passes the target
if remaining < 0:
return# no early exit: keep going to the leaf
Values can be negative. For [1, 2, null, -3] with target 0, the remaining target is already −1 after the root, so the search stops there, yet 1 + 2 + (−3) is exactly 0 and [[1, 2, -3]] is lost. The early return also skips the pop.
Forgetting to pop
dfs(node.left, remaining) dfs(node.right, remaining)
dfs(node.left, remaining) dfs(node.right, remaining) path.pop()
Values from a finished branch stay in the list. For [1, 2, 3] with target 4, the right leaf is reached with the list [1, 2, 3] and saves that instead of [1, 3].
Edge cases
root is None, so the search never starts and the result is [].
[5] with target 5The root has no children, so it is also a leaf, and [[5]] is returned.
Complexity
Path Sum II FAQ
What is the difference between Path Sum, Path Sum II and Path Sum III?
Path Sum (112) returns true or false: does any root-to-leaf path hit the target? Path Sum II (113) returns every such path. Path Sum III (437) counts downward paths that may start and end at any node, which needs prefix sums instead of a single remaining target.
Can it be done without recursion?
Yes. Push (node, remaining, path) onto a stack (or a queue for BFS), giving each child its own new path list. It avoids deep recursion on a 5,000-node chain, but it copies a path for every node instead of only for matches, so it uses more memory.