/** * File: preorder_traversal_iii_template.kt * Created Time: 2024-01-25 * Author: curtishd (1023632660@qq.com) */ package chapter_backtracking.preorder_traversal_iii_template import utils.TreeNode import utils.printTree /* 判断当前状态是否为解 */ fun isSolution(state: MutableList): Boolean { return state.isNotEmpty() && state[state.size - 1]?._val == 7 } /* 记录解 */ fun recordSolution(state: MutableList?, res: MutableList?>) { res.add(state!!.toMutableList()) } /* 判断在当前状态下,该选择是否合法 */ fun isValid(state: MutableList?, choice: TreeNode?): Boolean { return choice != null && choice._val != 3 } /* 更新状态 */ fun makeChoice(state: MutableList, choice: TreeNode?) { state.add(choice) } /* 恢复状态 */ fun undoChoice(state: MutableList, choice: TreeNode?) { state.removeLast() } /* 回溯算法:例题三 */ fun backtrack( state: MutableList, choices: MutableList, res: MutableList?> ) { // 检查是否为解 if (isSolution(state)) { // 记录解 recordSolution(state, res) } // 遍历所有选择 for (choice in choices) { // 剪枝:检查选择是否合法 if (isValid(state, choice)) { // 尝试:做出选择,更新状态 makeChoice(state, choice) // 进行下一轮选择 backtrack(state, mutableListOf(choice!!.left, choice.right), res) // 回退:撤销选择,恢复到之前的状态 undoChoice(state, choice) } } } /* Driver Code */ fun main() { val root = TreeNode.listToTree(mutableListOf(1, 7, 3, 4, 5, 6, 7)) println("\n初始化二叉树") printTree(root) // 回溯算法 val res = mutableListOf?>() backtrack(mutableListOf(), mutableListOf(root), res) println("\n输出所有根节点到节点 7 的路径,要求路径中不包含值为 3 的节点") for (path in res) { val vals = mutableListOf() for (node in path!!) { if (node != null) { vals.add(node._val) } } println(vals) } }