mirror of
https://github.com/krahets/hello-algo.git
synced 2026-11-29 07:44:46 +08:00
Retranslate the complete English edition from Simplified Chinese (#2010)
* Retranslate the complete English edition from Simplified Chinese * Polish three English explanations after reader-focused review
This commit is contained in:
@@ -10,31 +10,31 @@ public class binary_tree_bfs {
|
||||
|
||||
/* Level-order traversal */
|
||||
List<int> LevelOrder(TreeNode root) {
|
||||
// Initialize queue, add root node
|
||||
// Initialize a queue and enqueue the root
|
||||
Queue<TreeNode> queue = new();
|
||||
queue.Enqueue(root);
|
||||
// Initialize a list to save the traversal sequence
|
||||
// Initialize a list to store the traversal sequence
|
||||
List<int> list = [];
|
||||
while (queue.Count != 0) {
|
||||
TreeNode node = queue.Dequeue(); // Dequeue
|
||||
list.Add(node.val!.Value); // Save node value
|
||||
TreeNode node = queue.Dequeue(); // Dequeue from the queue
|
||||
list.Add(node.val!.Value); // Store the node value
|
||||
if (node.left != null)
|
||||
queue.Enqueue(node.left); // Left child node enqueue
|
||||
queue.Enqueue(node.left); // Enqueue the left child
|
||||
if (node.right != null)
|
||||
queue.Enqueue(node.right); // Right child node enqueue
|
||||
queue.Enqueue(node.right); // Enqueue the right child
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void Test() {
|
||||
/* Initialize binary tree */
|
||||
// Here we use a function to generate a binary tree directly from an array
|
||||
/* Initialize the binary tree */
|
||||
// Use a helper function that builds a binary tree directly from an array
|
||||
TreeNode? root = TreeNode.ListToTree([1, 2, 3, 4, 5, 6, 7]);
|
||||
Console.WriteLine("\nInitialize binary tree\n");
|
||||
Console.WriteLine("\nInitialize the binary tree\n");
|
||||
PrintUtil.PrintTree(root);
|
||||
|
||||
List<int> list = LevelOrder(root!);
|
||||
Console.WriteLine("\nLevel-order traversal node print sequence = " + string.Join(",", list));
|
||||
Console.WriteLine("\nNode sequence from level-order traversal = " + string.Join(",", list));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user