Path Sum II
Given the roottargetSumtargetSum
Example 1
Input: root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22
Output: [[5,4,11,2],[5,8,4,5]]
Explanation: Both paths 5→4→11→2 and 5→8→4→5 sum to 22.
Output: [[5,4,11,2],[5,8,4,5]]
Explanation: Both paths 5→4→11→2 and 5→8→4→5 sum to 22.
Example 2
Input: root = [1,2,3], targetSum = 5
Output: []
Explanation: No root-to-leaf path sums to 5.
Output: []
Explanation: No root-to-leaf path sums to 5.
Constraints:
0 <= Number of nodes <= 5000-1000 <= Node.val <= 1000-1000 <= targetSum <= 1000
Solution: DFS Backtracking
We extend the Path Sum I approach to collect all valid paths instead of just checking existence.
A mutable currentPathcurrentPathcurrentPath
Algorithm:
- Add the current node's value to
.currentPath - Subtract the node's value from
.remaining - If a leaf is reached and
, add a copy ofremaining == 0 to results.currentPath - Recurse on left and right children.
- Backtrack: remove the last element from
before returning.currentPath
Java
public List<List<Integer>> pathSum(TreeNode root, int targetSum) {
List<List<Integer>> result = new ArrayList<>();
dfs(root, targetSum, new ArrayList<>(), result);
return result;
}
private void dfs(TreeNode node, int remaining,
List<Integer> currentPath,
List<List<Integer>> result) {
if (node == null) return;
currentPath.add(node.val);
remaining -= node.val;
if (node.left == null && node.right == null && remaining == 0) {
result.add(new ArrayList<>(currentPath)); // snapshot copy
}
dfs(node.left, remaining, currentPath, result);
dfs(node.right, remaining, currentPath, result);
// backtrack — remove current node before returning to parent
currentPath.remove(currentPath.size() - 1);
}
Note: Always add a copy of currentPath
Complexity Analysis
- Time: O(n2). We visit every node O(n) times; copying a path at a leaf takes O(n) in the worst case (skewed tree). Overall O(n2) worst case.
- Space: O(n).
holds at most h elements (tree height), plus O(h) recursion stack. Output list is not counted in auxiliary space.currentPath