The negative numbers part is the thing I almost missed.
Use DFS with backtracking to explore all root-to-leaf paths, maintaining the current path and remaining sum. When a leaf is reached, check if the remaining sum equals the leaf's value; if so, add a copy of the current path to the result. Backtrack by removing the current node before returning.
Pro tip: Mention that you copy the path when adding to results to avoid mutation issues, and note that the problem guarantees at most 5000 nodes, so recursion depth is safe. Also, clarify that paths must end at a leaf, not just any node.
Confirm that a valid path must start at the root and end at a leaf, and that node values can be negative. Ask if the tree is binary and if the result should include paths as lists of integers.
Explain that DFS is ideal for exploring all root-to-leaf paths. Use a recursive function that carries the current path and the remaining target sum.
At each node, add its value to the path and subtract it from the remaining sum. If the node is a leaf and the remaining sum is zero, add a copy of the path to the result. Otherwise, recurse on left and right children.
After exploring both children, remove the current node from the path to restore the state for other branches. This ensures the path list is correctly maintained.
State that time complexity is O(N^2) in the worst case (e.g., a skewed tree) due to copying paths, but O(N) if we ignore copying. Space complexity is O(N) for the recursion stack and path storage.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.