Missing undo step on backtrack
Wrong move: Mutable state leaks between branches.
Usually fails on: Later branches inherit selections from earlier branches.
Fix: Always revert state changes immediately after recursive call.
Move from brute-force thinking to an efficient approach using backtracking strategy.
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. A leaf is a node with no children.
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: There are two paths whose sum equals targetSum: 5 + 4 + 11 + 2 = 22 5 + 8 + 4 + 5 = 22
Example 2:
Input: root = [1,2,3], targetSum = 5 Output: []
Example 3:
Input: root = [1,2], targetSum = 0 Output: []
Constraints:
[0, 5000].-1000 <= Node.val <= 1000-1000 <= targetSum <= 1000Problem summary: 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. A leaf is a node with no children.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Backtracking · Tree
[5,4,8,11,null,13,4,7,2,null,null,5,1] 22
[1,2,3] 5
[1,2] 0
path-sum)binary-tree-paths)path-sum-iii)path-sum-iv)step-by-step-directions-from-a-binary-tree-node-to-another)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #113: Path Sum II
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
private List<List<Integer>> ans = new ArrayList<>();
private List<Integer> t = new ArrayList<>();
public List<List<Integer>> pathSum(TreeNode root, int targetSum) {
dfs(root, targetSum);
return ans;
}
private void dfs(TreeNode root, int s) {
if (root == null) {
return;
}
s -= root.val;
t.add(root.val);
if (root.left == null && root.right == null && s == 0) {
ans.add(new ArrayList<>(t));
}
dfs(root.left, s);
dfs(root.right, s);
t.remove(t.size() - 1);
}
}
// Accepted solution for LeetCode #113: Path Sum II
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func pathSum(root *TreeNode, targetSum int) (ans [][]int) {
t := []int{}
var dfs func(*TreeNode, int)
dfs = func(root *TreeNode, s int) {
if root == nil {
return
}
s -= root.Val
t = append(t, root.Val)
if root.Left == nil && root.Right == nil && s == 0 {
ans = append(ans, slices.Clone(t))
}
dfs(root.Left, s)
dfs(root.Right, s)
t = t[:len(t)-1]
}
dfs(root, targetSum)
return
}
# Accepted solution for LeetCode #113: Path Sum II
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def pathSum(self, root: Optional[TreeNode], targetSum: int) -> List[List[int]]:
def dfs(root, s):
if root is None:
return
s += root.val
t.append(root.val)
if root.left is None and root.right is None and s == targetSum:
ans.append(t[:])
dfs(root.left, s)
dfs(root.right, s)
t.pop()
ans = []
t = []
dfs(root, 0)
return ans
// Accepted solution for LeetCode #113: Path Sum II
// Definition for a binary tree node.
// #[derive(Debug, PartialEq, Eq)]
// pub struct TreeNode {
// pub val: i32,
// pub left: Option<Rc<RefCell<TreeNode>>>,
// pub right: Option<Rc<RefCell<TreeNode>>>,
// }
//
// impl TreeNode {
// #[inline]
// pub fn new(val: i32) -> Self {
// TreeNode {
// val,
// left: None,
// right: None
// }
// }
// }
use std::cell::RefCell;
use std::rc::Rc;
impl Solution {
fn dfs(
root: Option<Rc<RefCell<TreeNode>>>,
paths: &mut Vec<i32>,
mut target_sum: i32,
res: &mut Vec<Vec<i32>>,
) {
if let Some(node) = root {
let mut node = node.borrow_mut();
target_sum -= node.val;
paths.push(node.val);
if node.left.is_none() && node.right.is_none() {
if target_sum == 0 {
res.push(paths.clone());
}
} else {
if node.left.is_some() {
Self::dfs(node.left.take(), paths, target_sum, res);
}
if node.right.is_some() {
Self::dfs(node.right.take(), paths, target_sum, res);
}
}
paths.pop();
}
}
pub fn path_sum(root: Option<Rc<RefCell<TreeNode>>>, target_sum: i32) -> Vec<Vec<i32>> {
let mut res = vec![];
let mut paths = vec![];
Self::dfs(root, &mut paths, target_sum, &mut res);
res
}
}
// Accepted solution for LeetCode #113: Path Sum II
// Auto-generated TypeScript example from java.
function exampleSolution(): void {
}
// Reference (java):
// // Accepted solution for LeetCode #113: Path Sum II
// /**
// * Definition for a binary tree node.
// * public class TreeNode {
// * int val;
// * TreeNode left;
// * TreeNode right;
// * TreeNode() {}
// * TreeNode(int val) { this.val = val; }
// * TreeNode(int val, TreeNode left, TreeNode right) {
// * this.val = val;
// * this.left = left;
// * this.right = right;
// * }
// * }
// */
// class Solution {
// private List<List<Integer>> ans = new ArrayList<>();
// private List<Integer> t = new ArrayList<>();
//
// public List<List<Integer>> pathSum(TreeNode root, int targetSum) {
// dfs(root, targetSum);
// return ans;
// }
//
// private void dfs(TreeNode root, int s) {
// if (root == null) {
// return;
// }
// s -= root.val;
// t.add(root.val);
// if (root.left == null && root.right == null && s == 0) {
// ans.add(new ArrayList<>(t));
// }
// dfs(root.left, s);
// dfs(root.right, s);
// t.remove(t.size() - 1);
// }
// }
Use this to step through a reusable interview workflow for this problem.
Generate every possible combination without any filtering. At each of n positions we choose from up to n options, giving nⁿ total candidates. Each candidate takes O(n) to validate. No pruning means we waste time on clearly invalid partial solutions.
Backtracking explores a decision tree, but prunes branches that violate constraints early. Worst case is still factorial or exponential, but pruning dramatically reduces the constant factor in practice. Space is the recursion depth (usually O(n) for n-level decisions).
Review these before coding to avoid predictable interview regressions.
Wrong move: Mutable state leaks between branches.
Usually fails on: Later branches inherit selections from earlier branches.
Fix: Always revert state changes immediately after recursive call.
Wrong move: Recursive traversal assumes children always exist.
Usually fails on: Leaf nodes throw errors or create wrong depth/path values.
Fix: Handle null/base cases before recursive transitions.