mirror of
https://github.com/krahets/hello-algo.git
synced 2024-12-27 12:06:29 +08:00
2655a2f66a
* Add kotlin code block for chapter_hashing * Add kotlin code block for chapter_heap. * Add kotlin code block for chapter_stack_and_queue and chapter_tree * fix indentation * Update binary_tree.md * style(kotlin): simplify code and improve readability. * simplify kt code for chapter_computational_complexity. * style(kotlin): replace ArrayList with MutableList. * Update subset_sum_i.kt Use kotlin api instead of java. * Update subset_sum_ii.kt use kotlin api instead of java * style(kotlin): replace ArrayList with mutablelist. --------- Co-authored-by: Yudong Jin <krahets@163.com>
82 lines
No EOL
2.3 KiB
Kotlin
82 lines
No EOL
2.3 KiB
Kotlin
/**
|
|
* 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<TreeNode?>): Boolean {
|
|
return state.isNotEmpty() && state[state.size - 1]?.value == 7
|
|
}
|
|
|
|
/* 记录解 */
|
|
fun recordSolution(state: MutableList<TreeNode?>?, res: MutableList<MutableList<TreeNode?>?>) {
|
|
res.add(state!!.toMutableList())
|
|
}
|
|
|
|
/* 判断在当前状态下,该选择是否合法 */
|
|
fun isValid(state: MutableList<TreeNode?>?, choice: TreeNode?): Boolean {
|
|
return choice != null && choice.value != 3
|
|
}
|
|
|
|
/* 更新状态 */
|
|
fun makeChoice(state: MutableList<TreeNode?>, choice: TreeNode?) {
|
|
state.add(choice)
|
|
}
|
|
|
|
/* 恢复状态 */
|
|
fun undoChoice(state: MutableList<TreeNode?>, choice: TreeNode?) {
|
|
state.removeLast()
|
|
}
|
|
|
|
/* 回溯算法:例题三 */
|
|
fun backtrack(
|
|
state: MutableList<TreeNode?>,
|
|
choices: MutableList<TreeNode?>,
|
|
res: MutableList<MutableList<TreeNode?>?>
|
|
) {
|
|
// 检查是否为解
|
|
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<MutableList<TreeNode?>?>()
|
|
backtrack(mutableListOf(), mutableListOf(root), res)
|
|
|
|
println("\n输出所有根节点到节点 7 的路径,要求路径中不包含值为 3 的节点")
|
|
for (path in res) {
|
|
val vals = mutableListOf<Int>()
|
|
for (node in path!!) {
|
|
if (node != null) {
|
|
vals.add(node.value)
|
|
}
|
|
}
|
|
println(vals)
|
|
}
|
|
} |