Skip to content

13.1   Backtracking algorithms

A backtracking algorithm solves a problem through exhaustive search. Starting from an initial state, it explores possible solutions by brute force, recording valid solutions as it finds them, until a solution is found or every possible choice has been tried without success.

Backtracking usually traverses the solution space through depth-first search. In the binary tree chapter, we learned that preorder, inorder, and postorder traversal are all forms of depth-first search. We will use preorder traversal to build a backtracking problem and explore the approach step by step.

Example 1

Given a binary tree, find and record every node with value \(7\). Return a list of these nodes.

Traverse the tree in preorder and check whether the current node's value is \(7\). If so, add that node's value to the result list res. The code and figure below show the process:

preorder_traversal_i_compact.py
def pre_order(root: TreeNode):
    """Preorder traversal: example 1"""
    if root is None:
        return
    if root.val == 7:
        # Record the solution
        res.append(root)
    pre_order(root.left)
    pre_order(root.right)
preorder_traversal_i_compact.cpp
/* Preorder traversal: example 1 */
void preOrder(TreeNode *root) {
    if (root == nullptr) {
        return;
    }
    if (root->val == 7) {
        // Record the solution
        res.push_back(root);
    }
    preOrder(root->left);
    preOrder(root->right);
}
preorder_traversal_i_compact.java
/* Preorder traversal: example 1 */
void preOrder(TreeNode root) {
    if (root == null) {
        return;
    }
    if (root.val == 7) {
        // Record the solution
        res.add(root);
    }
    preOrder(root.left);
    preOrder(root.right);
}
preorder_traversal_i_compact.cs
/* Preorder traversal: example 1 */
void PreOrder(TreeNode? root) {
    if (root == null) {
        return;
    }
    if (root.val == 7) {
        // Record the solution
        res.Add(root);
    }
    PreOrder(root.left);
    PreOrder(root.right);
}
preorder_traversal_i_compact.go
/* Preorder traversal: example 1 */
func preOrderI(root *TreeNode, res *[]*TreeNode) {
    if root == nil {
        return
    }
    if (root.Val).(int) == 7 {
        // Record the solution
        *res = append(*res, root)
    }
    preOrderI(root.Left, res)
    preOrderI(root.Right, res)
}
preorder_traversal_i_compact.swift
/* Preorder traversal: example 1 */
func preOrder(root: TreeNode?) {
    guard let root = root else {
        return
    }
    if root.val == 7 {
        // Record the solution
        res.append(root)
    }
    preOrder(root: root.left)
    preOrder(root: root.right)
}
preorder_traversal_i_compact.js
/* Preorder traversal: example 1 */
function preOrder(root, res) {
    if (root === null) {
        return;
    }
    if (root.val === 7) {
        // Record the solution
        res.push(root);
    }
    preOrder(root.left, res);
    preOrder(root.right, res);
}
preorder_traversal_i_compact.ts
/* Preorder traversal: example 1 */
function preOrder(root: TreeNode | null, res: TreeNode[]): void {
    if (root === null) {
        return;
    }
    if (root.val === 7) {
        // Record the solution
        res.push(root);
    }
    preOrder(root.left, res);
    preOrder(root.right, res);
}
preorder_traversal_i_compact.dart
/* Preorder traversal: example 1 */
void preOrder(TreeNode? root, List<TreeNode> res) {
  if (root == null) {
    return;
  }
  if (root.val == 7) {
    // Record the solution
    res.add(root);
  }
  preOrder(root.left, res);
  preOrder(root.right, res);
}
preorder_traversal_i_compact.rs
/* Preorder traversal: example 1 */
fn pre_order(res: &mut Vec<Rc<RefCell<TreeNode>>>, root: Option<&Rc<RefCell<TreeNode>>>) {
    if root.is_none() {
        return;
    }
    if let Some(node) = root {
        if node.borrow().val == 7 {
            // Record the solution
            res.push(node.clone());
        }
        pre_order(res, node.borrow().left.as_ref());
        pre_order(res, node.borrow().right.as_ref());
    }
}
preorder_traversal_i_compact.c
/* Preorder traversal: example 1 */
void preOrder(TreeNode *root) {
    if (root == NULL) {
        return;
    }
    if (root->val == 7) {
        // Record the solution
        res[resSize++] = root;
    }
    preOrder(root->left);
    preOrder(root->right);
}
preorder_traversal_i_compact.kt
/* Preorder traversal: example 1 */
fun preOrder(root: TreeNode?) {
    if (root == null) {
        return
    }
    if (root._val == 7) {
        // Record the solution
        res!!.add(root)
    }
    preOrder(root.left)
    preOrder(root.right)
}
preorder_traversal_i_compact.rb
### Preorder traversal: example 1 ###
def pre_order(root)
  return unless root

  # Record the solution
  $res << root if root.val == 7

  pre_order(root.left)
  pre_order(root.right)
end
Visualize execution

Figure 13-1   Finding nodes during preorder traversal

Description of Figure 13-1: Finding and recording all nodes with value 7 during preorder traversal

Traverse the seven nodes in root-left-right preorder, matching and recording the root's left child 7 followed by node 3's right child 7.

The binary tree has seven nodes: root 1 has left and right children 7 and 3; the left node 7 has left and right children 4 and 5; node 3 has left and right children 6 and another 7; nodes 4, 5, 6, and the right 7 are all leaf nodes.

The problem requires finding and recording every node in the tree with value 7 rather than stopping at the first match. The two cyan-highlighted 7s lie in different branches and represent two distinct node objects.

Green dashed arrows trace the depth-first traversal contour around the binary tree, while the orange dot to the left of each node marks the preorder visit point: check the node's value immediately upon entering the node, then recurse into its left subtree and right subtree in turn.

The full-tree preorder visit sequence is [1, 7, 4, 5, 3, 6, 7]. Whenever a node with value 7 is reached, it is appended to the result list and traversal continues through the remaining branches; thus after matching the left node 7, the search still visits its children 4 and 5.

The code records two node objects in res, in order of discovery: the root's left child 7 and node 3's right child 7; displayed by node value, this is [7, 7], corresponding to two nodes at different positions in the tree.

13.1.1   Trying choices and backtracking

Backtracking gets its name from its pattern of trying choices and undoing them while searching the solution space. When the search reaches a state that cannot advance or lead to a valid solution, it undoes the last choice, returns to the previous state, and tries other possibilities.

In Example 1, visiting a node is a trial, while a return after passing a leaf or returning to a parent is backtracking.

Backtracking involves more than returning from a function. To see why, let's extend Example 1.

Example 2

Find every node with value \(7\) in a binary tree, and return the paths from the root to those nodes.

Extend the first solution with a list path recording the current path of visited nodes. When a node with value \(7\) is reached, append a copy of path to the result list res. After traversal, res contains all solutions. The code is as follows:

preorder_traversal_ii_compact.py
def pre_order(root: TreeNode):
    """Preorder traversal: example 2"""
    if root is None:
        return
    # Try
    path.append(root)
    if root.val == 7:
        # Record the solution
        res.append(list(path))
    pre_order(root.left)
    pre_order(root.right)
    # Backtrack
    path.pop()
preorder_traversal_ii_compact.cpp
/* Preorder traversal: example 2 */
void preOrder(TreeNode *root) {
    if (root == nullptr) {
        return;
    }
    // Try
    path.push_back(root);
    if (root->val == 7) {
        // Record the solution
        res.push_back(path);
    }
    preOrder(root->left);
    preOrder(root->right);
    // Backtrack
    path.pop_back();
}
preorder_traversal_ii_compact.java
/* Preorder traversal: example 2 */
void preOrder(TreeNode root) {
    if (root == null) {
        return;
    }
    // Try
    path.add(root);
    if (root.val == 7) {
        // Record the solution
        res.add(new ArrayList<>(path));
    }
    preOrder(root.left);
    preOrder(root.right);
    // Backtrack
    path.remove(path.size() - 1);
}
preorder_traversal_ii_compact.cs
/* Preorder traversal: example 2 */
void PreOrder(TreeNode? root) {
    if (root == null) {
        return;
    }
    // Try
    path.Add(root);
    if (root.val == 7) {
        // Record the solution
        res.Add(new List<TreeNode>(path));
    }
    PreOrder(root.left);
    PreOrder(root.right);
    // Backtrack
    path.RemoveAt(path.Count - 1);
}
preorder_traversal_ii_compact.go
/* Preorder traversal: example 2 */
func preOrderII(root *TreeNode, res *[][]*TreeNode, path *[]*TreeNode) {
    if root == nil {
        return
    }
    // Try
    *path = append(*path, root)
    if root.Val.(int) == 7 {
        // Record the solution
        *res = append(*res, append([]*TreeNode{}, *path...))
    }
    preOrderII(root.Left, res, path)
    preOrderII(root.Right, res, path)
    // Backtrack
    *path = (*path)[:len(*path)-1]
}
preorder_traversal_ii_compact.swift
/* Preorder traversal: example 2 */
func preOrder(root: TreeNode?) {
    guard let root = root else {
        return
    }
    // Try
    path.append(root)
    if root.val == 7 {
        // Record the solution
        res.append(path)
    }
    preOrder(root: root.left)
    preOrder(root: root.right)
    // Backtrack
    path.removeLast()
}
preorder_traversal_ii_compact.js
/* Preorder traversal: example 2 */
function preOrder(root, path, res) {
    if (root === null) {
        return;
    }
    // Try
    path.push(root);
    if (root.val === 7) {
        // Record the solution
        res.push([...path]);
    }
    preOrder(root.left, path, res);
    preOrder(root.right, path, res);
    // Backtrack
    path.pop();
}
preorder_traversal_ii_compact.ts
/* Preorder traversal: example 2 */
function preOrder(
    root: TreeNode | null,
    path: TreeNode[],
    res: TreeNode[][]
): void {
    if (root === null) {
        return;
    }
    // Try
    path.push(root);
    if (root.val === 7) {
        // Record the solution
        res.push([...path]);
    }
    preOrder(root.left, path, res);
    preOrder(root.right, path, res);
    // Backtrack
    path.pop();
}
preorder_traversal_ii_compact.dart
/* Preorder traversal: example 2 */
void preOrder(
  TreeNode? root,
  List<TreeNode> path,
  List<List<TreeNode>> res,
) {
  if (root == null) {
    return;
  }

  // Try
  path.add(root);
  if (root.val == 7) {
    // Record the solution
    res.add(List.from(path));
  }
  preOrder(root.left, path, res);
  preOrder(root.right, path, res);
  // Backtrack
  path.removeLast();
}
preorder_traversal_ii_compact.rs
/* Preorder traversal: example 2 */
fn pre_order(
    res: &mut Vec<Vec<Rc<RefCell<TreeNode>>>>,
    path: &mut Vec<Rc<RefCell<TreeNode>>>,
    root: Option<&Rc<RefCell<TreeNode>>>,
) {
    if root.is_none() {
        return;
    }
    if let Some(node) = root {
        // Try
        path.push(node.clone());
        if node.borrow().val == 7 {
            // Record the solution
            res.push(path.clone());
        }
        pre_order(res, path, node.borrow().left.as_ref());
        pre_order(res, path, node.borrow().right.as_ref());
        // Backtrack
        path.pop();
    }
}
preorder_traversal_ii_compact.c
/* Preorder traversal: example 2 */
void preOrder(TreeNode *root) {
    if (root == NULL) {
        return;
    }
    // Try
    path[pathSize++] = root;
    if (root->val == 7) {
        // Record the solution
        for (int i = 0; i < pathSize; ++i) {
            res[resSize][i] = path[i];
        }
        resSize++;
    }
    preOrder(root->left);
    preOrder(root->right);
    // Backtrack
    pathSize--;
}
preorder_traversal_ii_compact.kt
/* Preorder traversal: example 2 */
fun preOrder(root: TreeNode?) {
    if (root == null) {
        return
    }
    // Try
    path!!.add(root)
    if (root._val == 7) {
        // Record the solution
        res!!.add(path!!.toMutableList())
    }
    preOrder(root.left)
    preOrder(root.right)
    // Backtrack
    path!!.removeAt(path!!.size - 1)
}
preorder_traversal_ii_compact.rb
### Preorder traversal: example 2 ###
def pre_order(root)
  return unless root

  # Try
  $path << root

  # Record the solution
  $res << $path.dup if root.val == 7

  pre_order(root.left)
  pre_order(root.right)

  # Backtrack
  $path.pop
end
Visualize execution

Each time we try a node, append it to path. Before backtracking, pop that node from path to restore the state from before the trial.

As Figure 13-2 illustrates, trying a choice and backtracking are like moving forward and undoing that move: they are inverse operations.

Figure 13-2   Trying choices and backtracking

Description of Figure 13-2: Trying choices and backtracking: saving paths to 7

Append each node to path on entry and pop it on exit; copy and save path on each of the two matches with 7, leaving path empty when the search ends.

The parent-child relationships of the fixed tree are shown in the table below. The left 7 is the left child of root 1, and the right 7 is the right child of node 3.

Traverse the entire tree in "root → left → right" preorder to find the paths from root node 1 to every node with value 7.

The state list path maintains the active path from the root to the current node in real time, and the result list res stores a copy of path each time a 7 is matched. The figure abbreviates path and res using node values, whereas the accompanying code stores node objects and list copies list(path); the left and right nodes with value 7 are distinct objects.

Each forward "trial" entering a node appends it to the end of path; if the current node has value 7, a copy of path is appended to res; before "backtracking" out of a node, it is popped from the end of path to restore the path from before entering that node.

Fixed binary tree: distinguishing the two nodes with value 7
NodeLeft childRight child
Root 1Left 73
Left 745
36Right 7
4NoneNone
5NoneNone
6NoneNone
Right 7NoneNone
  1. Enter root 1

    Start at root node 1 and append 1 to path. Node value 1 is not the target 7, so path = [1] and res = []; both subtrees remain to be explored, and the search next enters the left 7.

  2. Match left 7 and save path

    Enter the left 7 from root 1, append 7, and match the target, saving a copy of the current path to res. Now path = [1, 7] and res = [[1, 7]]; continue exploring the left 7's children 4 and 5.

  3. Enter node 4

    Enter the left leaf node 4 of the left 7 and append 4 to path; it is not the target 7. Now path = [1, 7, 4] and res = [[1, 7]]; node 5 and the root's right subtree remain to be explored.

  4. Exit 4 and return to left 7

    Both empty subtrees of leaf node 4 have returned, so pop 4 from path and return to its parent, the left 7. Now path = [1, 7] and res = [[1, 7]]; node 4 is finished, and the right child 5 is explored next.

  5. Enter node 5

    Enter the right leaf node 5 of the left 7 and append 5 to path; the previously popped sibling node 4 does not linger. Now path = [1, 7, 5] and res = [[1, 7]]; all nodes in the left subtree have been visited, and the root's right subtree remains to be explored.

  6. Exit 5 and left 7

    After finishing node 5, first pop 5 to return to the left 7; since both of its subtrees are now complete, pop the left 7 as well to return to root 1. This frame combines those two backtracking steps. Now path = [1] and res = [[1, 7]]; next explore the root's right child 3.

  7. Enter node 3

    Enter the right child 3 of root 1 and append 3 to path; it is not the target 7. Now path = [1, 3] and res = [[1, 7]]; continue exploring node 3's left leaf 6 and right leaf 7.

  8. Enter node 6

    Enter the left leaf node 6 of node 3 and append 6 to path; it is not the target 7. Now path = [1, 3, 6] and res = [[1, 7]]; across the whole tree, only node 3's right child 7 remains unvisited.

  9. Exit 6 and return to 3

    Leaf node 6 is finished, so pop 6 from path and return to parent node 3. Now path = [1, 3] and res = [[1, 7]]; node 6 is complete, and the right 7 is explored next.

  10. Match right 7 and save path

    Enter the right leaf node 7 of node 3, append 7 to match the second target node, and save a copy of path to res. Now path = [1, 3, 7] and res = [[1, 7], [1, 3, 7]]; all nodes have been visited, leaving only the final cleanup backtracking.

  11. Exit right 7, 3, and root 1

    After finishing the right 7, pop the right 7, parent node 3, and root 1 from path in order. This frame combines the three final backtracking steps, ending the search with path = [] and res = [[1, 7], [1, 3, 7]]; the saved copies remain intact.

Because res stores independent list copies of path taken at each match, popping during backtracking modifies only the active path without altering previously saved solutions.

13.1.2   Pruning

Complex backtracking problems usually have one or more constraints, which can often be used for pruning.

Example 3

Find every node with value \(7\) in a binary tree and return the paths from the root to those nodes, requiring that no path contain a node with value \(3\).

To enforce this constraint, add a pruning step: when a node with value \(3\) is encountered, return immediately without searching farther. The code is as follows:

preorder_traversal_iii_compact.py
def pre_order(root: TreeNode):
    """Preorder traversal: example 3"""
    # Prune
    if root is None or root.val == 3:
        return
    # Try
    path.append(root)
    if root.val == 7:
        # Record the solution
        res.append(list(path))
    pre_order(root.left)
    pre_order(root.right)
    # Backtrack
    path.pop()
preorder_traversal_iii_compact.cpp
/* Preorder traversal: example 3 */
void preOrder(TreeNode *root) {
    // Prune
    if (root == nullptr || root->val == 3) {
        return;
    }
    // Try
    path.push_back(root);
    if (root->val == 7) {
        // Record the solution
        res.push_back(path);
    }
    preOrder(root->left);
    preOrder(root->right);
    // Backtrack
    path.pop_back();
}
preorder_traversal_iii_compact.java
/* Preorder traversal: example 3 */
void preOrder(TreeNode root) {
    // Prune
    if (root == null || root.val == 3) {
        return;
    }
    // Try
    path.add(root);
    if (root.val == 7) {
        // Record the solution
        res.add(new ArrayList<>(path));
    }
    preOrder(root.left);
    preOrder(root.right);
    // Backtrack
    path.remove(path.size() - 1);
}
preorder_traversal_iii_compact.cs
/* Preorder traversal: example 3 */
void PreOrder(TreeNode? root) {
    // Prune
    if (root == null || root.val == 3) {
        return;
    }
    // Try
    path.Add(root);
    if (root.val == 7) {
        // Record the solution
        res.Add(new List<TreeNode>(path));
    }
    PreOrder(root.left);
    PreOrder(root.right);
    // Backtrack
    path.RemoveAt(path.Count - 1);
}
preorder_traversal_iii_compact.go
/* Preorder traversal: example 3 */
func preOrderIII(root *TreeNode, res *[][]*TreeNode, path *[]*TreeNode) {
    // Prune
    if root == nil || root.Val == 3 {
        return
    }
    // Try
    *path = append(*path, root)
    if root.Val.(int) == 7 {
        // Record the solution
        *res = append(*res, append([]*TreeNode{}, *path...))
    }
    preOrderIII(root.Left, res, path)
    preOrderIII(root.Right, res, path)
    // Backtrack
    *path = (*path)[:len(*path)-1]
}
preorder_traversal_iii_compact.swift
/* Preorder traversal: example 3 */
func preOrder(root: TreeNode?) {
    // Prune
    guard let root = root, root.val != 3 else {
        return
    }
    // Try
    path.append(root)
    if root.val == 7 {
        // Record the solution
        res.append(path)
    }
    preOrder(root: root.left)
    preOrder(root: root.right)
    // Backtrack
    path.removeLast()
}
preorder_traversal_iii_compact.js
/* Preorder traversal: example 3 */
function preOrder(root, path, res) {
    // Prune
    if (root === null || root.val === 3) {
        return;
    }
    // Try
    path.push(root);
    if (root.val === 7) {
        // Record the solution
        res.push([...path]);
    }
    preOrder(root.left, path, res);
    preOrder(root.right, path, res);
    // Backtrack
    path.pop();
}
preorder_traversal_iii_compact.ts
/* Preorder traversal: example 3 */
function preOrder(
    root: TreeNode | null,
    path: TreeNode[],
    res: TreeNode[][]
): void {
    // Prune
    if (root === null || root.val === 3) {
        return;
    }
    // Try
    path.push(root);
    if (root.val === 7) {
        // Record the solution
        res.push([...path]);
    }
    preOrder(root.left, path, res);
    preOrder(root.right, path, res);
    // Backtrack
    path.pop();
}
preorder_traversal_iii_compact.dart
/* Preorder traversal: example 3 */
void preOrder(
  TreeNode? root,
  List<TreeNode> path,
  List<List<TreeNode>> res,
) {
  if (root == null || root.val == 3) {
    return;
  }

  // Try
  path.add(root);
  if (root.val == 7) {
    // Record the solution
    res.add(List.from(path));
  }
  preOrder(root.left, path, res);
  preOrder(root.right, path, res);
  // Backtrack
  path.removeLast();
}
preorder_traversal_iii_compact.rs
/* Preorder traversal: example 3 */
fn pre_order(
    res: &mut Vec<Vec<Rc<RefCell<TreeNode>>>>,
    path: &mut Vec<Rc<RefCell<TreeNode>>>,
    root: Option<&Rc<RefCell<TreeNode>>>,
) {
    // Prune
    if root.is_none() || root.as_ref().unwrap().borrow().val == 3 {
        return;
    }
    if let Some(node) = root {
        // Try
        path.push(node.clone());
        if node.borrow().val == 7 {
            // Record the solution
            res.push(path.clone());
        }
        pre_order(res, path, node.borrow().left.as_ref());
        pre_order(res, path, node.borrow().right.as_ref());
        // Backtrack
        path.pop();
    }
}
preorder_traversal_iii_compact.c
/* Preorder traversal: example 3 */
void preOrder(TreeNode *root) {
    // Prune
    if (root == NULL || root->val == 3) {
        return;
    }
    // Try
    path[pathSize++] = root;
    if (root->val == 7) {
        // Record the solution
        for (int i = 0; i < pathSize; i++) {
            res[resSize][i] = path[i];
        }
        resSize++;
    }
    preOrder(root->left);
    preOrder(root->right);
    // Backtrack
    pathSize--;
}
preorder_traversal_iii_compact.kt
/* Preorder traversal: example 3 */
fun preOrder(root: TreeNode?) {
    // Prune
    if (root == null || root._val == 3) {
        return
    }
    // Try
    path!!.add(root)
    if (root._val == 7) {
        // Record the solution
        res!!.add(path!!.toMutableList())
    }
    preOrder(root.left)
    preOrder(root.right)
    // Backtrack
    path!!.removeAt(path!!.size - 1)
}
preorder_traversal_iii_compact.rb
### Preorder traversal: example 3 ###
def pre_order(root)
  # Prune
  return if !root || root.val == 3

  # Try
  $path.append(root)

  # Record the solution
  $res << $path.dup if root.val == 7

  pre_order(root.left)
  pre_order(root.right)

  # Backtrack
  $path.pop
end
Visualize execution

Pruning is an apt name. As shown in Figure 13-3, we cut off search branches that violate the constraints, avoiding pointless trials and improving efficiency.

Figure 13-3   Pruning according to constraints

Description of Figure 13-3: Pruning with the constraint that paths exclude 3

When searching for root-to-7 paths that do not contain 3, return early at node 3 to prune its subtree, skipping node 6 and the right 7.

The binary tree has seven nodes: root 1 has left and right children 7 and 3; the left node 7 has left and right children 4 and 5; node 3 has left and right children 6 and another 7; nodes 4, 5, 6, and the right 7 are all leaf nodes.

The problem asks for paths from the root to all nodes with value 7 such that no path contains a node with value 3. Any path extending downward past node 3 necessarily violates this constraint.

The left subtree contains no 3, so preorder traversal records the valid path [1, 7] upon visiting the left node 7 and continues checking its children 4 and 5.

Reaching the root's right child 3 triggers the constraint, so the algorithm returns immediately before appending 3 to path and does not recurse into its children 6 and the right 7. The figure marks node 3 in red and uses a scissors icon with a faded subtree to show that this search branch is skipped; the tree nodes themselves are not deleted from the data structure.

The only path satisfying the constraint is [1, 7]. Although the right leaf node 7 equals the target value 7, the path [1, 3, 7] from the root to it passes through the forbidden node 3 and is therefore ruled out during pruning.

13.1.3   A code template

We will now extract the core pattern of trying, undoing, and pruning into a more general backtracking template.

In the following template, state is the current problem state, and choices contains the choices available from that state:

def backtrack(state: State, choices: list[Choice], res: list[State]):
    """Backtracking template"""
    # Check whether the state is a solution
    if is_solution(state):
        # Record the solution
        record_solution(state, res)
        # Stop searching this branch
        return
    # Try every choice
    for choice in choices:
        # Prune: check whether the choice is valid
        if is_valid(state, choice):
            # Try: make the choice and update the state
            make_choice(state, choice)
            backtrack(state, choices, res)
            # Backtrack: undo the choice and restore the previous state
            undo_choice(state, choice)
/* Backtracking template */
void backtrack(State *state, vector<Choice *> &choices, vector<State *> &res) {
    // Check whether the state is a solution
    if (isSolution(state)) {
        // Record the solution
        recordSolution(state, res);
        // Stop searching this branch
        return;
    }
    // Try every choice
    for (Choice *choice : choices) {
        // Prune: check whether the choice is valid
        if (isValid(state, choice)) {
            // Try: make the choice and update the state
            makeChoice(state, choice);
            backtrack(state, choices, res);
            // Backtrack: undo the choice and restore the previous state
            undoChoice(state, choice);
        }
    }
}
/* Backtracking template */
void backtrack(State state, List<Choice> choices, List<State> res) {
    // Check whether the state is a solution
    if (isSolution(state)) {
        // Record the solution
        recordSolution(state, res);
        // Stop searching this branch
        return;
    }
    // Try every choice
    for (Choice choice : choices) {
        // Prune: check whether the choice is valid
        if (isValid(state, choice)) {
            // Try: make the choice and update the state
            makeChoice(state, choice);
            backtrack(state, choices, res);
            // Backtrack: undo the choice and restore the previous state
            undoChoice(state, choice);
        }
    }
}
/* Backtracking template */
void Backtrack(State state, List<Choice> choices, List<State> res) {
    // Check whether the state is a solution
    if (IsSolution(state)) {
        // Record the solution
        RecordSolution(state, res);
        // Stop searching this branch
        return;
    }
    // Try every choice
    foreach (Choice choice in choices) {
        // Prune: check whether the choice is valid
        if (IsValid(state, choice)) {
            // Try: make the choice and update the state
            MakeChoice(state, choice);
            Backtrack(state, choices, res);
            // Backtrack: undo the choice and restore the previous state
            UndoChoice(state, choice);
        }
    }
}
/* Backtracking template */
func backtrack(state *State, choices []Choice, res *[]State) {
    // Check whether the state is a solution
    if isSolution(state) {
        // Record the solution
        recordSolution(state, res)
        // Stop searching this branch
        return
    }
    // Try every choice
    for _, choice := range choices {
        // Prune: check whether the choice is valid
        if isValid(state, choice) {
            // Try: make the choice and update the state
            makeChoice(state, choice)
            backtrack(state, choices, res)
            // Backtrack: undo the choice and restore the previous state
            undoChoice(state, choice)
        }
    }
}
/* Backtracking template */
func backtrack(state: inout State, choices: [Choice], res: inout [State]) {
    // Check whether the state is a solution
    if isSolution(state: state) {
        // Record the solution
        recordSolution(state: state, res: &res)
        // Stop searching this branch
        return
    }
    // Try every choice
    for choice in choices {
        // Prune: check whether the choice is valid
        if isValid(state: state, choice: choice) {
            // Try: make the choice and update the state
            makeChoice(state: &state, choice: choice)
            backtrack(state: &state, choices: choices, res: &res)
            // Backtrack: undo the choice and restore the previous state
            undoChoice(state: &state, choice: choice)
        }
    }
}
/* Backtracking template */
function backtrack(state, choices, res) {
    // Check whether the state is a solution
    if (isSolution(state)) {
        // Record the solution
        recordSolution(state, res);
        // Stop searching this branch
        return;
    }
    // Try every choice
    for (let choice of choices) {
        // Prune: check whether the choice is valid
        if (isValid(state, choice)) {
            // Try: make the choice and update the state
            makeChoice(state, choice);
            backtrack(state, choices, res);
            // Backtrack: undo the choice and restore the previous state
            undoChoice(state, choice);
        }
    }
}
/* Backtracking template */
function backtrack(state: State, choices: Choice[], res: State[]): void {
    // Check whether the state is a solution
    if (isSolution(state)) {
        // Record the solution
        recordSolution(state, res);
        // Stop searching this branch
        return;
    }
    // Try every choice
    for (let choice of choices) {
        // Prune: check whether the choice is valid
        if (isValid(state, choice)) {
            // Try: make the choice and update the state
            makeChoice(state, choice);
            backtrack(state, choices, res);
            // Backtrack: undo the choice and restore the previous state
            undoChoice(state, choice);
        }
    }
}
/* Backtracking template */
void backtrack(State state, List<Choice> choices, List<State> res) {
  // Check whether the state is a solution
  if (isSolution(state)) {
    // Record the solution
    recordSolution(state, res);
    // Stop searching this branch
    return;
  }
  // Try every choice
  for (Choice choice in choices) {
    // Prune: check whether the choice is valid
    if (isValid(state, choice)) {
      // Try: make the choice and update the state
      makeChoice(state, choice);
      backtrack(state, choices, res);
      // Backtrack: undo the choice and restore the previous state
      undoChoice(state, choice);
    }
  }
}
/* Backtracking template */
fn backtrack(state: &mut State, choices: &Vec<Choice>, res: &mut Vec<State>) {
    // Check whether the state is a solution
    if is_solution(state) {
        // Record the solution
        record_solution(state, res);
        // Stop searching this branch
        return;
    }
    // Try every choice
    for choice in choices {
        // Prune: check whether the choice is valid
        if is_valid(state, choice) {
            // Try: make the choice and update the state
            make_choice(state, choice);
            backtrack(state, choices, res);
            // Backtrack: undo the choice and restore the previous state
            undo_choice(state, choice);
        }
    }
}
/* Backtracking template */
void backtrack(State *state, Choice *choices, int numChoices, State *res, int numRes) {
    // Check whether the state is a solution
    if (isSolution(state)) {
        // Record the solution
        recordSolution(state, res, numRes);
        // Stop searching this branch
        return;
    }
    // Try every choice
    for (int i = 0; i < numChoices; i++) {
        // Prune: check whether the choice is valid
        if (isValid(state, &choices[i])) {
            // Try: make the choice and update the state
            makeChoice(state, &choices[i]);
            backtrack(state, choices, numChoices, res, numRes);
            // Backtrack: undo the choice and restore the previous state
            undoChoice(state, &choices[i]);
        }
    }
}
/* Backtracking template */
fun backtrack(state: State?, choices: List<Choice?>, res: List<State?>?) {
    // Check whether the state is a solution
    if (isSolution(state)) {
        // Record the solution
        recordSolution(state, res)
        // Stop searching this branch
        return
    }
    // Try every choice
    for (choice in choices) {
        // Prune: check whether the choice is valid
        if (isValid(state, choice)) {
            // Try: make the choice and update the state
            makeChoice(state, choice)
            backtrack(state, choices, res)
            // Backtrack: undo the choice and restore the previous state
            undoChoice(state, choice)
        }
    }
}
### Backtracking template ###
def backtrack(state, choices, res)
    # Check whether the state is a solution
    if is_solution?(state)
        # Record the solution
        record_solution(state, res)
        return
    end

    # Try every choice
    for choice in choices
        # Prune: check whether the choice is valid
        if is_valid?(state, choice)
            # Try: make the choice and update the state
            make_choice(state, choice)
            backtrack(state, choices, res)
            # Backtrack: undo the choice and restore the previous state
            undo_choice(state, choice)
        end
    end
end

Now apply this template to Example 3. The state is the current path of visited nodes, choices consists of the current node's left and right children, and res is the list of paths:

preorder_traversal_iii_template.py
def is_solution(state: list[TreeNode]) -> bool:
    """Check whether the current state is a solution"""
    return state and state[-1].val == 7

def record_solution(state: list[TreeNode], res: list[list[TreeNode]]):
    """Record the solution"""
    res.append(list(state))

def is_valid(state: list[TreeNode], choice: TreeNode) -> bool:
    """Check whether the choice is valid in the current state"""
    return choice is not None and choice.val != 3

def make_choice(state: list[TreeNode], choice: TreeNode):
    """Update the state"""
    state.append(choice)

def undo_choice(state: list[TreeNode], choice: TreeNode):
    """Restore the state"""
    state.pop()

def backtrack(
    state: list[TreeNode], choices: list[TreeNode], res: list[list[TreeNode]]
):
    """Backtracking: example 3"""
    # Check whether this is a solution
    if is_solution(state):
        # Record the solution
        record_solution(state, res)
    # Iterate over all choices
    for choice in choices:
        # Prune: check whether the choice is valid
        if is_valid(state, choice):
            # Try: make a choice and update the state
            make_choice(state, choice)
            # Proceed to the next choice
            backtrack(state, [choice.left, choice.right], res)
            # Backtrack: undo the choice and restore the previous state
            undo_choice(state, choice)
preorder_traversal_iii_template.cpp
/* Check whether the current state is a solution */
bool isSolution(vector<TreeNode *> &state) {
    return !state.empty() && state.back()->val == 7;
}

/* Record the solution */
void recordSolution(vector<TreeNode *> &state, vector<vector<TreeNode *>> &res) {
    res.push_back(state);
}

/* Check whether the choice is valid in the current state */
bool isValid(vector<TreeNode *> &state, TreeNode *choice) {
    return choice != nullptr && choice->val != 3;
}

/* Update the state */
void makeChoice(vector<TreeNode *> &state, TreeNode *choice) {
    state.push_back(choice);
}

/* Restore the state */
void undoChoice(vector<TreeNode *> &state, TreeNode *choice) {
    state.pop_back();
}

/* Backtracking: example 3 */
void backtrack(vector<TreeNode *> &state, vector<TreeNode *> &choices, vector<vector<TreeNode *>> &res) {
    // Check whether this is a solution
    if (isSolution(state)) {
        // Record the solution
        recordSolution(state, res);
    }
    // Iterate over all choices
    for (TreeNode *choice : choices) {
        // Prune: check whether the choice is valid
        if (isValid(state, choice)) {
            // Try: make a choice and update the state
            makeChoice(state, choice);
            // Proceed to the next choice
            vector<TreeNode *> nextChoices{choice->left, choice->right};
            backtrack(state, nextChoices, res);
            // Backtrack: undo the choice and restore the previous state
            undoChoice(state, choice);
        }
    }
}
preorder_traversal_iii_template.java
/* Check whether the current state is a solution */
boolean isSolution(List<TreeNode> state) {
    return !state.isEmpty() && state.get(state.size() - 1).val == 7;
}

/* Record the solution */
void recordSolution(List<TreeNode> state, List<List<TreeNode>> res) {
    res.add(new ArrayList<>(state));
}

/* Check whether the choice is valid in the current state */
boolean isValid(List<TreeNode> state, TreeNode choice) {
    return choice != null && choice.val != 3;
}

/* Update the state */
void makeChoice(List<TreeNode> state, TreeNode choice) {
    state.add(choice);
}

/* Restore the state */
void undoChoice(List<TreeNode> state, TreeNode choice) {
    state.remove(state.size() - 1);
}

/* Backtracking: example 3 */
void backtrack(List<TreeNode> state, List<TreeNode> choices, List<List<TreeNode>> res) {
    // Check whether this is a solution
    if (isSolution(state)) {
        // Record the solution
        recordSolution(state, res);
    }
    // Iterate over all choices
    for (TreeNode choice : choices) {
        // Prune: check whether the choice is valid
        if (isValid(state, choice)) {
            // Try: make a choice and update the state
            makeChoice(state, choice);
            // Proceed to the next choice
            backtrack(state, Arrays.asList(choice.left, choice.right), res);
            // Backtrack: undo the choice and restore the previous state
            undoChoice(state, choice);
        }
    }
}
preorder_traversal_iii_template.cs
/* Check whether the current state is a solution */
bool IsSolution(List<TreeNode> state) {
    return state.Count != 0 && state[^1].val == 7;
}

/* Record the solution */
void RecordSolution(List<TreeNode> state, List<List<TreeNode>> res) {
    res.Add(new List<TreeNode>(state));
}

/* Check whether the choice is valid in the current state */
bool IsValid(List<TreeNode> state, TreeNode choice) {
    return choice != null && choice.val != 3;
}

/* Update the state */
void MakeChoice(List<TreeNode> state, TreeNode choice) {
    state.Add(choice);
}

/* Restore the state */
void UndoChoice(List<TreeNode> state, TreeNode choice) {
    state.RemoveAt(state.Count - 1);
}

/* Backtracking: example 3 */
void Backtrack(List<TreeNode> state, List<TreeNode> choices, List<List<TreeNode>> res) {
    // Check whether this is a solution
    if (IsSolution(state)) {
        // Record the solution
        RecordSolution(state, res);
    }
    // Iterate over all choices
    foreach (TreeNode choice in choices) {
        // Prune: check whether the choice is valid
        if (IsValid(state, choice)) {
            // Try: make a choice and update the state
            MakeChoice(state, choice);
            // Proceed to the next choice
            Backtrack(state, [choice.left!, choice.right!], res);
            // Backtrack: undo the choice and restore the previous state
            UndoChoice(state, choice);
        }
    }
}
preorder_traversal_iii_template.go
/* Check whether the current state is a solution */
func isSolution(state *[]*TreeNode) bool {
    return len(*state) != 0 && (*state)[len(*state)-1].Val == 7
}

/* Record the solution */
func recordSolution(state *[]*TreeNode, res *[][]*TreeNode) {
    *res = append(*res, append([]*TreeNode{}, *state...))
}

/* Check whether the choice is valid in the current state */
func isValid(state *[]*TreeNode, choice *TreeNode) bool {
    return choice != nil && choice.Val != 3
}

/* Update the state */
func makeChoice(state *[]*TreeNode, choice *TreeNode) {
    *state = append(*state, choice)
}

/* Restore the state */
func undoChoice(state *[]*TreeNode, choice *TreeNode) {
    *state = (*state)[:len(*state)-1]
}

/* Backtracking: example 3 */
func backtrackIII(state *[]*TreeNode, choices *[]*TreeNode, res *[][]*TreeNode) {
    // Check whether this is a solution
    if isSolution(state) {
        // Record the solution
        recordSolution(state, res)
    }
    // Iterate over all choices
    for _, choice := range *choices {
        // Prune: check whether the choice is valid
        if isValid(state, choice) {
            // Try: make a choice and update the state
            makeChoice(state, choice)
            // Proceed to the next choice
            temp := make([]*TreeNode, 0)
            temp = append(temp, choice.Left, choice.Right)
            backtrackIII(state, &temp, res)
            // Backtrack: undo the choice and restore the previous state
            undoChoice(state, choice)
        }
    }
}
preorder_traversal_iii_template.swift
/* Check whether the current state is a solution */
func isSolution(state: [TreeNode]) -> Bool {
    !state.isEmpty && state.last!.val == 7
}

/* Record the solution */
func recordSolution(state: [TreeNode], res: inout [[TreeNode]]) {
    res.append(state)
}

/* Check whether the choice is valid in the current state */
func isValid(state: [TreeNode], choice: TreeNode?) -> Bool {
    choice != nil && choice!.val != 3
}

/* Update the state */
func makeChoice(state: inout [TreeNode], choice: TreeNode) {
    state.append(choice)
}

/* Restore the state */
func undoChoice(state: inout [TreeNode], choice: TreeNode) {
    state.removeLast()
}

/* Backtracking: example 3 */
func backtrack(state: inout [TreeNode], choices: [TreeNode], res: inout [[TreeNode]]) {
    // Check whether this is a solution
    if isSolution(state: state) {
        recordSolution(state: state, res: &res)
    }
    // Iterate over all choices
    for choice in choices {
        // Prune: check whether the choice is valid
        if isValid(state: state, choice: choice) {
            // Try: make a choice and update the state
            makeChoice(state: &state, choice: choice)
            // Proceed to the next choice
            backtrack(state: &state, choices: [choice.left, choice.right].compactMap { $0 }, res: &res)
            // Backtrack: undo the choice and restore the previous state
            undoChoice(state: &state, choice: choice)
        }
    }
}
preorder_traversal_iii_template.js
/* Check whether the current state is a solution */
function isSolution(state) {
    return state && state[state.length - 1]?.val === 7;
}

/* Record the solution */
function recordSolution(state, res) {
    res.push([...state]);
}

/* Check whether the choice is valid in the current state */
function isValid(state, choice) {
    return choice !== null && choice.val !== 3;
}

/* Update the state */
function makeChoice(state, choice) {
    state.push(choice);
}

/* Restore the state */
function undoChoice(state) {
    state.pop();
}

/* Backtracking: example 3 */
function backtrack(state, choices, res) {
    // Check whether this is a solution
    if (isSolution(state)) {
        // Record the solution
        recordSolution(state, res);
    }
    // Iterate over all choices
    for (const choice of choices) {
        // Prune: check whether the choice is valid
        if (isValid(state, choice)) {
            // Try: make a choice and update the state
            makeChoice(state, choice);
            // Proceed to the next choice
            backtrack(state, [choice.left, choice.right], res);
            // Backtrack: undo the choice and restore the previous state
            undoChoice(state);
        }
    }
}
preorder_traversal_iii_template.ts
/* Check whether the current state is a solution */
function isSolution(state: TreeNode[]): boolean {
    return state && state[state.length - 1]?.val === 7;
}

/* Record the solution */
function recordSolution(state: TreeNode[], res: TreeNode[][]): void {
    res.push([...state]);
}

/* Check whether the choice is valid in the current state */
function isValid(state: TreeNode[], choice: TreeNode): boolean {
    return choice !== null && choice.val !== 3;
}

/* Update the state */
function makeChoice(state: TreeNode[], choice: TreeNode): void {
    state.push(choice);
}

/* Restore the state */
function undoChoice(state: TreeNode[]): void {
    state.pop();
}

/* Backtracking: example 3 */
function backtrack(
    state: TreeNode[],
    choices: TreeNode[],
    res: TreeNode[][]
): void {
    // Check whether this is a solution
    if (isSolution(state)) {
        // Record the solution
        recordSolution(state, res);
    }
    // Iterate over all choices
    for (const choice of choices) {
        // Prune: check whether the choice is valid
        if (isValid(state, choice)) {
            // Try: make a choice and update the state
            makeChoice(state, choice);
            // Proceed to the next choice
            backtrack(state, [choice.left, choice.right], res);
            // Backtrack: undo the choice and restore the previous state
            undoChoice(state);
        }
    }
}
preorder_traversal_iii_template.dart
/* Check whether the current state is a solution */
bool isSolution(List<TreeNode> state) {
  return state.isNotEmpty && state.last.val == 7;
}

/* Record the solution */
void recordSolution(List<TreeNode> state, List<List<TreeNode>> res) {
  res.add(List.from(state));
}

/* Check whether the choice is valid in the current state */
bool isValid(List<TreeNode> state, TreeNode? choice) {
  return choice != null && choice.val != 3;
}

/* Update the state */
void makeChoice(List<TreeNode> state, TreeNode? choice) {
  state.add(choice!);
}

/* Restore the state */
void undoChoice(List<TreeNode> state, TreeNode? choice) {
  state.removeLast();
}

/* Backtracking: example 3 */
void backtrack(
  List<TreeNode> state,
  List<TreeNode?> choices,
  List<List<TreeNode>> res,
) {
  // Check whether this is a solution
  if (isSolution(state)) {
    // Record the solution
    recordSolution(state, res);
  }
  // Iterate over all choices
  for (TreeNode? choice in choices) {
    // Prune: check whether the choice is valid
    if (isValid(state, choice)) {
      // Try: make a choice and update the state
      makeChoice(state, choice);
      // Proceed to the next choice
      backtrack(state, [choice!.left, choice.right], res);
      // Backtrack: undo the choice and restore the previous state
      undoChoice(state, choice);
    }
  }
}
preorder_traversal_iii_template.rs
/* Check whether the current state is a solution */
fn is_solution(state: &mut Vec<Rc<RefCell<TreeNode>>>) -> bool {
    return !state.is_empty() && state.last().unwrap().borrow().val == 7;
}

/* Record the solution */
fn record_solution(
    state: &mut Vec<Rc<RefCell<TreeNode>>>,
    res: &mut Vec<Vec<Rc<RefCell<TreeNode>>>>,
) {
    res.push(state.clone());
}

/* Check whether the choice is valid in the current state */
fn is_valid(_: &mut Vec<Rc<RefCell<TreeNode>>>, choice: Option<&Rc<RefCell<TreeNode>>>) -> bool {
    return choice.is_some() && choice.unwrap().borrow().val != 3;
}

/* Update the state */
fn make_choice(state: &mut Vec<Rc<RefCell<TreeNode>>>, choice: Rc<RefCell<TreeNode>>) {
    state.push(choice);
}

/* Restore the state */
fn undo_choice(state: &mut Vec<Rc<RefCell<TreeNode>>>, _: Rc<RefCell<TreeNode>>) {
    state.pop();
}

/* Backtracking: example 3 */
fn backtrack(
    state: &mut Vec<Rc<RefCell<TreeNode>>>,
    choices: &Vec<Option<&Rc<RefCell<TreeNode>>>>,
    res: &mut Vec<Vec<Rc<RefCell<TreeNode>>>>,
) {
    // Check whether this is a solution
    if is_solution(state) {
        // Record the solution
        record_solution(state, res);
    }
    // Iterate over all choices
    for &choice in choices.iter() {
        // Prune: check whether the choice is valid
        if is_valid(state, choice) {
            // Try: make a choice and update the state
            make_choice(state, choice.unwrap().clone());
            // Proceed to the next choice
            backtrack(
                state,
                &vec![
                    choice.unwrap().borrow().left.as_ref(),
                    choice.unwrap().borrow().right.as_ref(),
                ],
                res,
            );
            // Backtrack: undo the choice and restore the previous state
            undo_choice(state, choice.unwrap().clone());
        }
    }
}
preorder_traversal_iii_template.c
/* Check whether the current state is a solution */
bool isSolution(void) {
    return pathSize > 0 && path[pathSize - 1]->val == 7;
}

/* Record the solution */
void recordSolution(void) {
    for (int i = 0; i < pathSize; i++) {
        res[resSize][i] = path[i];
    }
    resSize++;
}

/* Check whether the choice is valid in the current state */
bool isValid(TreeNode *choice) {
    return choice != NULL && choice->val != 3;
}

/* Update the state */
void makeChoice(TreeNode *choice) {
    path[pathSize++] = choice;
}

/* Restore the state */
void undoChoice(void) {
    pathSize--;
}

/* Backtracking: example 3 */
void backtrack(TreeNode *choices[2]) {
    // Check whether this is a solution
    if (isSolution()) {
        // Record the solution
        recordSolution();
    }
    // Iterate over all choices
    for (int i = 0; i < 2; i++) {
        TreeNode *choice = choices[i];
        // Prune: check whether the choice is valid
        if (isValid(choice)) {
            // Try: make a choice and update the state
            makeChoice(choice);
            // Proceed to the next choice
            TreeNode *nextChoices[2] = {choice->left, choice->right};
            backtrack(nextChoices);
            // Backtrack: undo the choice and restore the previous state
            undoChoice();
        }
    }
}
preorder_traversal_iii_template.kt
/* Check whether the current state is a solution */
fun isSolution(state: MutableList<TreeNode?>): Boolean {
    return state.isNotEmpty() && state[state.size - 1]?._val == 7
}

/* Record the solution */
fun recordSolution(state: MutableList<TreeNode?>?, res: MutableList<MutableList<TreeNode?>?>) {
    res.add(state!!.toMutableList())
}

/* Check whether the choice is valid in the current state */
fun isValid(state: MutableList<TreeNode?>?, choice: TreeNode?): Boolean {
    return choice != null && choice._val != 3
}

/* Update the state */
fun makeChoice(state: MutableList<TreeNode?>, choice: TreeNode?) {
    state.add(choice)
}

/* Restore the state */
fun undoChoice(state: MutableList<TreeNode?>, choice: TreeNode?) {
    state.removeLast()
}

/* Backtracking: example 3 */
fun backtrack(
    state: MutableList<TreeNode?>,
    choices: MutableList<TreeNode?>,
    res: MutableList<MutableList<TreeNode?>?>
) {
    // Check whether this is a solution
    if (isSolution(state)) {
        // Record the solution
        recordSolution(state, res)
    }
    // Iterate over all choices
    for (choice in choices) {
        // Prune: check whether the choice is valid
        if (isValid(state, choice)) {
            // Try: make a choice and update the state
            makeChoice(state, choice)
            // Proceed to the next choice
            backtrack(state, mutableListOf(choice!!.left, choice.right), res)
            // Backtrack: undo the choice and restore the previous state
            undoChoice(state, choice)
        }
    }
}
preorder_traversal_iii_template.rb
### Check whether the current state is a solution ###
def is_solution?(state)
  !state.empty? && state.last.val == 7
end

### Record the solution ###
def record_solution(state, res)
  res << state.dup
end

### Check whether the choice is valid in the current state ###
def is_valid?(state, choice)
  choice && choice.val != 3
end

### Update the state ###
def make_choice(state, choice)
  state << choice
end

### Restore the state ###
def undo_choice(state, choice)
  state.pop
end

### Backtracking: example 3 ###
def backtrack(state, choices, res)
  # Check whether this is a solution
  record_solution(state, res) if is_solution?(state)

  # Iterate over all choices
  for choice in choices
    # Prune: check whether the choice is valid
    if is_valid?(state, choice)
      # Try: make a choice and update the state
      make_choice(state, choice)
      # Proceed to the next choice
      backtrack(state, [choice.left, choice.right], res)
      # Backtrack: undo the choice and restore the previous state
      undo_choice(state, choice)
    end
  end
end
Visualize execution

The problem requires us to keep searching after finding a node with value \(7\), so remove the return after recording a solution. Figure 13-4 compares the searches with and without that return.

Figure 13-4   Comparing searches with and without return

Description of Figure 13-4: Whether to keep searching after recording a solution

When valid solutions may still exist among a target node's descendants, returning immediately after recording a solution misses deeper solutions; that early return must be removed so the search continues.

The task is to find all paths starting at the root, ending at a node with value 7, and containing no node with value 3. The binary tree shown has 8 nodes: root 1 has left and right children 7 and 3; the left (shallower) 7 has left and right children 4 and 5, with a deeper left child 7 beneath 4; and 3 has left and right children 6 and the right 7. Compared with the seven-node input in the earlier example and accompanying Python code, this figure adds the deeper left child 7 beneath 4; on that seven-node tree, removing the return after recording a solution would not add any solutions.

What is compared here is whether to execute return immediately after recording a solution to end the current recursive branch. Pruning that stops descending at node 3 and normal backtracking after visiting a node's children remain unchanged in both approaches; the template code implements that pruning by rejecting choices with value 3.

The left and right panels respectively keep and remove the early return after recording a solution. The table below compares which nodes each search visits and resulting paths; in the right branch, both panels stop descending upon reaching the red node 3, skipping 6 and the right 7.

Handling after recording a solution and resulting paths
ApproachNodes visited nextPaths found
Left panel: keep early returnStops at the shallower 7 without visiting its descendants 4, 5, and the deeper 7[1, 7]
Right panel: remove early returnContinues after recording the shallower 7 to visit 4, the deeper 7, and 5[1, 7]; [1, 7, 4, 7]

Only by continuing deeper after recording the shallower 7 can the right panel discover the deeper 7 among its descendants; ending the current branch early misses that solution.

The template-based implementation is more verbose than the preorder version, but more general. Many backtracking problems can be solved within this template: define state and choices for the problem, then implement each helper method.

13.1.4   Common terminology

Table 13-1 summarizes common backtracking terms and illustrates them with Example 3, helping us analyze problems more clearly.

Table 13-1   Common backtracking terminology

Term Definition Example 3
Solution An answer satisfying the problem's conditions; there may be one or more All paths from the root to a node valued \(7\) that satisfy the constraint
Constraint A condition restricting which solutions are feasible, often used for pruning The path contains no node valued \(3\)
State The situation at a particular point, including the choices already made The current path of visited nodes, stored in path
Trial Exploring the solution space by making a choice, updating the state, and checking for a solution Recursively visit a left or right child, append it to path, and check whether its value is \(7\)
Backtracking Undoing an earlier choice and returning to the previous state when a constraint cannot be met Stop searching and return after passing a leaf, finishing a node, or encountering value \(3\)
Pruning Using problem properties and constraints to avoid pointless search paths and improve efficiency Stop searching when a node valued \(3\) is encountered

Tip

Concepts such as problems, solutions, and states are shared by divide and conquer, backtracking, dynamic programming, greedy algorithms, and other approaches.

13.1.5   Strengths and limitations

Backtracking is fundamentally a depth-first search that tries possible solutions until it finds ones meeting the conditions. It can find all possible solutions and can be highly efficient with effective pruning.

However, for large or complex problems, backtracking may be unacceptably slow.

  • Time: Backtracking often explores every possibility in the state space, leading to exponential or factorial time complexity.
  • Space: Recursive calls must preserve state, such as the path and auxiliary pruning variables. Deep recursion can therefore require substantial space.

Even so, backtracking remains the best approach for some search and constraint satisfaction problems. When we cannot predict which choices lead to valid solutions, we must explore every possibility. The key is then to improve efficiency, commonly through two techniques.

  • Pruning: Avoid paths that cannot produce a solution, saving time and space.
  • Heuristic search: Use strategies or estimates to explore paths most likely to yield valid solutions first.

13.1.6   Typical backtracking problems

Backtracking can solve many search, constraint satisfaction, and combinatorial optimization problems.

Search problems seek solutions satisfying specified conditions.

  • Permutations: Given a collection, find every possible ordering of its elements.
  • Subset sum: Given a collection and a target sum, find every subset whose sum equals the target.
  • Tower of Hanoi: Given three rods and disks of different sizes, move all disks from one rod to another, moving only one at a time and never placing a larger disk on a smaller one.

Constraint satisfaction problems seek solutions meeting all constraints.

  • \(n\)-queens: Place \(n\) queens on an \(n \times n\) chessboard so that no two attack each other.
  • Sudoku: Fill a \(9 \times 9\) grid with digits \(1\) ~ \(9\) so that no digit repeats in any row, column, or \(3 \times 3\) subgrid.
  • Graph coloring: Color an undirected graph's vertices with as few colors as possible, ensuring adjacent vertices have different colors.

Combinatorial optimization problems seek an optimal solution satisfying conditions within a space of combinations.

  • 0-1 knapsack: Given items with values and weights and a knapsack, choose items to maximize total value within the capacity limit.
  • Traveling salesperson: Starting at a vertex in a graph, visit every other vertex exactly once and return to the start along the shortest possible route.
  • Maximum clique: Given an undirected graph, find its largest complete subgraph, in which every pair of vertices is connected by an edge.

For many combinatorial optimization problems, backtracking is not the best approach.

  • 0-1 knapsack is usually solved more efficiently with dynamic programming.
  • The traveling salesperson problem is a well-known NP-hard problem. Common approaches include genetic algorithms and ant colony algorithms.
  • Maximum clique is a classic graph theory problem that can be approached with heuristics such as greedy algorithms.
Share your insights, questions, or suggestions in the comments