This commit is contained in:
krahets
2024-05-07 16:35:22 +08:00
parent 5c7d2c7f17
commit bd54cd096b
6 changed files with 26 additions and 26 deletions
@@ -6,13 +6,13 @@ comments: true
<u>Backtracking algorithm</u> is a method to solve problems by exhaustive search, where the core idea is to start from an initial state and brute force all possible solutions, recording the correct ones until a solution is found or all possible choices are exhausted without finding a solution.
Backtracking typically employs "depth-first search" to traverse the solution space. In the "Binary Tree" chapter, we mentioned that preorder, inorder, and postorder traversals are all depth-first searches. Next, we use preorder traversal to construct a backtracking problem to gradually understand the workings of the backtracking algorithm.
Backtracking typically employs "depth-first search" to traverse the solution space. In the "Binary Tree" chapter, we mentioned that pre-order, in-order, and post-order traversals are all depth-first searches. Next, we use pre-order traversal to construct a backtracking problem to gradually understand the workings of the backtracking algorithm.
!!! question "Example One"
Given a binary tree, search and record all nodes with a value of $7$, please return a list of nodes.
For this problem, we traverse this tree in preorder and check if the current node's value is $7$. If it is, we add the node's value to the result list `res`. The relevant process is shown in Figure 13-1:
For this problem, we traverse this tree in pre-order and check if the current node's value is $7$. If it is, we add the node's value to the result list `res`. The relevant process is shown in Figure 13-1:
=== "Python"
@@ -128,9 +128,9 @@ For this problem, we traverse this tree in preorder and check if the current nod
[class]{}-[func]{preOrder}
```
![Searching nodes in preorder traversal](backtracking_algorithm.assets/preorder_find_nodes.png){ class="animation-figure" }
![Searching nodes in pre-order traversal](backtracking_algorithm.assets/preorder_find_nodes.png){ class="animation-figure" }
<p align="center"> Figure 13-1 &nbsp; Searching nodes in preorder traversal </p>
<p align="center"> Figure 13-1 &nbsp; Searching nodes in pre-order traversal </p>
## 13.1.1 &nbsp; Trying and retreating
@@ -1110,7 +1110,7 @@ As per the requirements, after finding a node with a value of $7$, the search sh
<p align="center"> Figure 13-4 &nbsp; Comparison of retaining and removing the return in the search process </p>
Compared to the implementation based on preorder traversal, the code implementation based on the backtracking algorithm framework seems verbose, but it has better universality. In fact, **many backtracking problems can be solved within this framework**. We just need to define `state` and `choices` according to the specific problem and implement the methods in the framework.
Compared to the implementation based on pre-order traversal, the code implementation based on the backtracking algorithm framework seems verbose, but it has better universality. In fact, **many backtracking problems can be solved within this framework**. We just need to define `state` and `choices` according to the specific problem and implement the methods in the framework.
## 13.1.4 &nbsp; Common terminology