mirror of
https://github.com/krahets/hello-algo.git
synced 2026-07-24 12:06:07 +00:00
fix(csharp): Modify method name to PascalCase, simplify new expression (#840)
* Modify method name to PascalCase(array and linked list) * Modify method name to PascalCase(backtracking) * Modify method name to PascalCase(computational complexity) * Modify method name to PascalCase(divide and conquer) * Modify method name to PascalCase(dynamic programming) * Modify method name to PascalCase(graph) * Modify method name to PascalCase(greedy) * Modify method name to PascalCase(hashing) * Modify method name to PascalCase(heap) * Modify method name to PascalCase(searching) * Modify method name to PascalCase(sorting) * Modify method name to PascalCase(stack and queue) * Modify method name to PascalCase(tree) * local check
This commit is contained in:
@@ -8,47 +8,47 @@ namespace hello_algo.chapter_backtracking;
|
||||
|
||||
public class preorder_traversal_iii_template {
|
||||
/* 判断当前状态是否为解 */
|
||||
static bool isSolution(List<TreeNode> state) {
|
||||
static bool IsSolution(List<TreeNode> state) {
|
||||
return state.Count != 0 && state[^1].val == 7;
|
||||
}
|
||||
|
||||
/* 记录解 */
|
||||
static void recordSolution(List<TreeNode> state, List<List<TreeNode>> res) {
|
||||
static void RecordSolution(List<TreeNode> state, List<List<TreeNode>> res) {
|
||||
res.Add(new List<TreeNode>(state));
|
||||
}
|
||||
|
||||
/* 判断在当前状态下,该选择是否合法 */
|
||||
static bool isValid(List<TreeNode> state, TreeNode choice) {
|
||||
static bool IsValid(List<TreeNode> state, TreeNode choice) {
|
||||
return choice != null && choice.val != 3;
|
||||
}
|
||||
|
||||
/* 更新状态 */
|
||||
static void makeChoice(List<TreeNode> state, TreeNode choice) {
|
||||
static void MakeChoice(List<TreeNode> state, TreeNode choice) {
|
||||
state.Add(choice);
|
||||
}
|
||||
|
||||
/* 恢复状态 */
|
||||
static void undoChoice(List<TreeNode> state, TreeNode choice) {
|
||||
static 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) {
|
||||
static void Backtrack(List<TreeNode> state, List<TreeNode> choices, List<List<TreeNode>> res) {
|
||||
// 检查是否为解
|
||||
if (isSolution(state)) {
|
||||
if (IsSolution(state)) {
|
||||
// 记录解
|
||||
recordSolution(state, res);
|
||||
RecordSolution(state, res);
|
||||
}
|
||||
// 遍历所有选择
|
||||
foreach (TreeNode choice in choices) {
|
||||
// 剪枝:检查选择是否合法
|
||||
if (isValid(state, choice)) {
|
||||
if (IsValid(state, choice)) {
|
||||
// 尝试:做出选择,更新状态
|
||||
makeChoice(state, choice);
|
||||
MakeChoice(state, choice);
|
||||
// 进行下一轮选择
|
||||
backtrack(state, new List<TreeNode> { choice.left, choice.right }, res);
|
||||
Backtrack(state, new List<TreeNode> { choice.left, choice.right }, res);
|
||||
// 回退:撤销选择,恢复到之前的状态
|
||||
undoChoice(state, choice);
|
||||
UndoChoice(state, choice);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -60,9 +60,9 @@ public class preorder_traversal_iii_template {
|
||||
PrintUtil.PrintTree(root);
|
||||
|
||||
// 回溯算法
|
||||
List<List<TreeNode>> res = new List<List<TreeNode>>();
|
||||
List<TreeNode> choices = new List<TreeNode>() { root };
|
||||
backtrack(new List<TreeNode>(), choices, res);
|
||||
List<List<TreeNode>> res = new();
|
||||
List<TreeNode> choices = new() { root };
|
||||
Backtrack(new List<TreeNode>(), choices, res);
|
||||
|
||||
Console.WriteLine("\n输出所有根节点到节点 7 的路径,要求路径中不包含值为 3 的节点");
|
||||
foreach (List<TreeNode> path in res) {
|
||||
|
||||
Reference in New Issue
Block a user