/** * File: preorder_traversal_iii_template.cs * Created Time: 2023-04-17 * Author: hpstory (hpstory1024@163.com) */ namespace hello_algo.chapter_backtracking; public class preorder_traversal_iii_template { /* 判断当前状态是否为解 */ bool IsSolution(List state) { return state.Count != 0 && state[^1].val == 7; } /* 记录解 */ void RecordSolution(List state, List> res) { res.Add(new List(state)); } /* 判断在当前状态下,该选择是否合法 */ bool IsValid(List state, TreeNode choice) { return choice != null && choice.val != 3; } /* 更新状态 */ void MakeChoice(List state, TreeNode choice) { state.Add(choice); } /* 恢复状态 */ void UndoChoice(List state, TreeNode choice) { state.RemoveAt(state.Count - 1); } /* 回溯算法:例题三 */ void Backtrack(List state, List choices, List> res) { // 检查是否为解 if (IsSolution(state)) { // 记录解 RecordSolution(state, res); } // 遍历所有选择 foreach (TreeNode choice in choices) { // 剪枝:检查选择是否合法 if (IsValid(state, choice)) { // 尝试:做出选择,更新状态 MakeChoice(state, choice); // 进行下一轮选择 Backtrack(state, [choice.left!, choice.right!], res); // 回退:撤销选择,恢复到之前的状态 UndoChoice(state, choice); } } } [Test] public void Test() { TreeNode? root = TreeNode.ListToTree([1, 7, 3, 4, 5, 6, 7]); Console.WriteLine("\n初始化二叉树"); PrintUtil.PrintTree(root); // 回溯算法 List> res = []; List choices = [root!]; Backtrack([], choices, res); Console.WriteLine("\n输出所有根节点到节点 7 的路径,要求路径中不包含值为 3 的节点"); foreach (List path in res) { PrintUtil.PrintList(path.Select(p => p.val).ToList()); } } }