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