mirror of
https://github.com/krahets/hello-algo.git
synced 2025-11-02 21:24:53 +08: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:
@ -9,15 +9,15 @@ namespace hello_algo.chapter_tree;
|
||||
public class binary_tree_bfs {
|
||||
|
||||
/* 层序遍历 */
|
||||
public List<int> LevelOrder(TreeNode root) {
|
||||
List<int> LevelOrder(TreeNode root) {
|
||||
// 初始化队列,加入根节点
|
||||
Queue<TreeNode> queue = new();
|
||||
queue.Enqueue(root);
|
||||
// 初始化一个列表,用于保存遍历序列
|
||||
List<int> list = new();
|
||||
List<int> list = [];
|
||||
while (queue.Count != 0) {
|
||||
TreeNode node = queue.Dequeue(); // 队列出队
|
||||
list.Add(node.val); // 保存节点值
|
||||
list.Add(node.val!.Value); // 保存节点值
|
||||
if (node.left != null)
|
||||
queue.Enqueue(node.left); // 左子节点入队
|
||||
if (node.right != null)
|
||||
@ -30,11 +30,11 @@ public class binary_tree_bfs {
|
||||
public void Test() {
|
||||
/* 初始化二叉树 */
|
||||
// 这里借助了一个从数组直接生成二叉树的函数
|
||||
TreeNode? root = TreeNode.ListToTree(new List<int?> { 1, 2, 3, 4, 5, 6, 7 });
|
||||
TreeNode? root = TreeNode.ListToTree([1, 2, 3, 4, 5, 6, 7]);
|
||||
Console.WriteLine("\n初始化二叉树\n");
|
||||
PrintUtil.PrintTree(root);
|
||||
|
||||
List<int> list = LevelOrder(root);
|
||||
List<int> list = LevelOrder(root!);
|
||||
Console.WriteLine("\n层序遍历的节点打印序列 = " + string.Join(",", list));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user