mirror of
https://github.com/krahets/hello-algo.git
synced 2024-12-26 12:26:29 +08:00
bad759b4f8
in backtracking algorithm. Add the typedef to docs.
77 lines
2.4 KiB
Java
77 lines
2.4 KiB
Java
/**
|
|
* File: preorder_traversal_iii_template.java
|
|
* Created Time: 2023-04-16
|
|
* Author: Krahets (krahets@163.com)
|
|
*/
|
|
|
|
package chapter_backtracking;
|
|
|
|
import include.*;
|
|
import java.util.*;
|
|
|
|
public class preorder_traversal_iii_template {
|
|
/* 判断当前状态是否为解 */
|
|
static boolean isSolution(List<TreeNode> state) {
|
|
return !state.isEmpty() && state.get(state.size() - 1).val == 7;
|
|
}
|
|
|
|
/* 记录解 */
|
|
static void recordSolution(List<TreeNode> state, List<List<TreeNode>> res) {
|
|
res.add(new ArrayList<>(state));
|
|
}
|
|
|
|
/* 判断在当前状态下,该选择是否合法 */
|
|
static boolean isValid(List<TreeNode> state, TreeNode choice) {
|
|
return choice != null && choice.val != 3;
|
|
}
|
|
|
|
/* 更新状态 */
|
|
static void makeChoice(List<TreeNode> state, TreeNode choice) {
|
|
state.add(choice);
|
|
}
|
|
|
|
/* 恢复状态 */
|
|
static void undoChoice(List<TreeNode> state, TreeNode choice) {
|
|
state.remove(state.size() - 1);
|
|
}
|
|
|
|
/* 回溯算法:例题三 */
|
|
static void backtrack(List<TreeNode> state, List<TreeNode> choices, List<List<TreeNode>> res) {
|
|
// 检查是否为解
|
|
if (isSolution(state)) {
|
|
// 记录解
|
|
recordSolution(state, res);
|
|
return;
|
|
}
|
|
// 遍历所有选择
|
|
for (TreeNode choice : choices) {
|
|
// 剪枝:检查选择是否合法
|
|
if (isValid(state, choice)) {
|
|
// 尝试:做出选择,更新状态
|
|
makeChoice(state, choice);
|
|
backtrack(state, Arrays.asList(choice.left, choice.right), res);
|
|
// 回退:撤销选择,恢复到之前的状态
|
|
undoChoice(state, choice);
|
|
}
|
|
}
|
|
}
|
|
|
|
public static void main(String[] args) {
|
|
TreeNode root = TreeNode.listToTree(Arrays.asList(1, 7, 3, 4, 5, 6, 7));
|
|
System.out.println("\n初始化二叉树");
|
|
PrintUtil.printTree(root);
|
|
|
|
// 回溯算法
|
|
List<List<TreeNode>> res = new ArrayList<>();
|
|
backtrack(new ArrayList<>(), Arrays.asList(root), res);
|
|
|
|
System.out.println("\n输出所有根节点到节点 7 的路径,要求路径中不包含值为 3 的节点");
|
|
for (List<TreeNode> path : res) {
|
|
List<Integer> vals = new ArrayList<>();
|
|
for (TreeNode node : path) {
|
|
vals.add(node.val);
|
|
}
|
|
System.out.println(vals);
|
|
}
|
|
}
|
|
}
|