2023-05-11 23:46:12 +08:00
|
|
|
/**
|
|
|
|
* File: preorder_traversal_iii_template.js
|
|
|
|
* Created Time: 2023-05-09
|
|
|
|
* Author: Justin (xiefahit@gmail.com)
|
|
|
|
*/
|
|
|
|
|
|
|
|
const { arrToTree } = require('../modules/TreeNode');
|
|
|
|
const { printTree } = require('../modules/PrintUtil');
|
|
|
|
|
|
|
|
/* 判断当前状态是否为解 */
|
|
|
|
function isSolution(state) {
|
2023-05-14 01:40:19 +08:00
|
|
|
return state && state[state.length - 1]?.val === 7;
|
2023-05-11 23:46:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 记录解 */
|
|
|
|
function recordSolution(state, res) {
|
2023-05-14 01:40:19 +08:00
|
|
|
res.push([...state]);
|
2023-05-11 23:46:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 判断在当前状态下,该选择是否合法 */
|
|
|
|
function isValid(state, choice) {
|
2023-05-14 01:40:19 +08:00
|
|
|
return choice !== null && choice.val !== 3;
|
2023-05-11 23:46:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 更新状态 */
|
|
|
|
function makeChoice(state, choice) {
|
2023-05-14 01:40:19 +08:00
|
|
|
state.push(choice);
|
2023-05-11 23:46:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 恢复状态 */
|
|
|
|
function undoChoice(state) {
|
2023-05-14 01:40:19 +08:00
|
|
|
state.pop();
|
2023-05-11 23:46:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 回溯算法:例题三 */
|
|
|
|
function backtrack(state, choices, res) {
|
2023-05-14 01:40:19 +08:00
|
|
|
// 检查是否为解
|
|
|
|
if (isSolution(state)) {
|
|
|
|
// 记录解
|
|
|
|
recordSolution(state, res);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// 遍历所有选择
|
|
|
|
for (const choice of choices) {
|
|
|
|
// 剪枝:检查选择是否合法
|
|
|
|
if (isValid(state, choice)) {
|
|
|
|
// 尝试:做出选择,更新状态
|
|
|
|
makeChoice(state, choice);
|
|
|
|
// 进行下一轮选择
|
|
|
|
backtrack(state, [choice.left, choice.right], res);
|
|
|
|
// 回退:撤销选择,恢复到之前的状态
|
|
|
|
undoChoice(state);
|
|
|
|
}
|
2023-05-11 23:46:12 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Driver Code
|
|
|
|
const root = arrToTree([1, 7, 3, 4, 5, 6, 7]);
|
2023-05-14 01:40:19 +08:00
|
|
|
console.log('\n初始化二叉树');
|
2023-05-11 23:46:12 +08:00
|
|
|
printTree(root);
|
|
|
|
|
|
|
|
// 回溯算法
|
|
|
|
const res = [];
|
|
|
|
backtrack([], [root], res);
|
|
|
|
|
2023-05-14 01:40:19 +08:00
|
|
|
console.log('\n输出所有根节点到节点 7 的路径,要求路径中不包含值为 3 的节点');
|
|
|
|
res.forEach((path) => {
|
|
|
|
console.log(path.map((node) => node.val));
|
2023-05-11 23:46:12 +08:00
|
|
|
});
|