mirror of
https://github.com/krahets/hello-algo.git
synced 2026-08-10 19:10:57 +00:00
feat(csharp) .NET 8.0 code migration (#966)
* .net 8.0 migration * update docs * revert change * revert change and update appendix docs * remove static * Update binary_search_insertion.cs * Update binary_search_insertion.cs * Update binary_search_edge.cs * Update binary_search_insertion.cs * Update binary_search_edge.cs --------- Co-authored-by: Yudong Jin <krahets@163.com>
This commit is contained in:
@@ -8,32 +8,32 @@ namespace hello_algo.chapter_backtracking;
|
||||
|
||||
public class preorder_traversal_iii_template {
|
||||
/* 判断当前状态是否为解 */
|
||||
static bool IsSolution(List<TreeNode> state) {
|
||||
bool IsSolution(List<TreeNode> state) {
|
||||
return state.Count != 0 && state[^1].val == 7;
|
||||
}
|
||||
|
||||
/* 记录解 */
|
||||
static void RecordSolution(List<TreeNode> state, List<List<TreeNode>> res) {
|
||||
void RecordSolution(List<TreeNode> state, List<List<TreeNode>> res) {
|
||||
res.Add(new List<TreeNode>(state));
|
||||
}
|
||||
|
||||
/* 判断在当前状态下,该选择是否合法 */
|
||||
static bool IsValid(List<TreeNode> state, TreeNode choice) {
|
||||
bool IsValid(List<TreeNode> state, TreeNode choice) {
|
||||
return choice != null && choice.val != 3;
|
||||
}
|
||||
|
||||
/* 更新状态 */
|
||||
static void MakeChoice(List<TreeNode> state, TreeNode choice) {
|
||||
void MakeChoice(List<TreeNode> state, TreeNode choice) {
|
||||
state.Add(choice);
|
||||
}
|
||||
|
||||
/* 恢复状态 */
|
||||
static void UndoChoice(List<TreeNode> state, TreeNode choice) {
|
||||
void UndoChoice(List<TreeNode> state, TreeNode choice) {
|
||||
state.RemoveAt(state.Count - 1);
|
||||
}
|
||||
|
||||
/* 回溯算法:例题三 */
|
||||
static void Backtrack(List<TreeNode> state, List<TreeNode> choices, List<List<TreeNode>> res) {
|
||||
void Backtrack(List<TreeNode> state, List<TreeNode> choices, List<List<TreeNode>> res) {
|
||||
// 检查是否为解
|
||||
if (IsSolution(state)) {
|
||||
// 记录解
|
||||
@@ -46,7 +46,7 @@ public class preorder_traversal_iii_template {
|
||||
// 尝试:做出选择,更新状态
|
||||
MakeChoice(state, choice);
|
||||
// 进行下一轮选择
|
||||
Backtrack(state, new List<TreeNode> { choice.left, choice.right }, res);
|
||||
Backtrack(state, [choice.left!, choice.right!], res);
|
||||
// 回退:撤销选择,恢复到之前的状态
|
||||
UndoChoice(state, choice);
|
||||
}
|
||||
@@ -55,14 +55,14 @@ public class preorder_traversal_iii_template {
|
||||
|
||||
[Test]
|
||||
public void Test() {
|
||||
TreeNode root = TreeNode.ListToTree(new List<int?> { 1, 7, 3, 4, 5, 6, 7 });
|
||||
TreeNode? root = TreeNode.ListToTree([1, 7, 3, 4, 5, 6, 7]);
|
||||
Console.WriteLine("\n初始化二叉树");
|
||||
PrintUtil.PrintTree(root);
|
||||
|
||||
// 回溯算法
|
||||
List<List<TreeNode>> res = new();
|
||||
List<TreeNode> choices = new() { root };
|
||||
Backtrack(new List<TreeNode>(), choices, res);
|
||||
List<List<TreeNode>> res = [];
|
||||
List<TreeNode> choices = [root!];
|
||||
Backtrack([], choices, res);
|
||||
|
||||
Console.WriteLine("\n输出所有根节点到节点 7 的路径,要求路径中不包含值为 3 的节点");
|
||||
foreach (List<TreeNode> path in res) {
|
||||
|
||||
Reference in New Issue
Block a user