mirror of
https://github.com/krahets/hello-algo.git
synced 2026-11-13 01:08:11 +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:
@@ -9,26 +9,26 @@ const { printTree } = require('../modules/PrintUtil');
|
||||
|
||||
/* Level-order traversal */
|
||||
function levelOrder(root) {
|
||||
// Initialize queue, add root node
|
||||
// Initialize a queue and enqueue the root
|
||||
const queue = [root];
|
||||
// Initialize a list to save the traversal sequence
|
||||
// Initialize a list to store the traversal sequence
|
||||
const list = [];
|
||||
while (queue.length) {
|
||||
let node = queue.shift(); // Dequeue
|
||||
list.push(node.val); // Save node value
|
||||
if (node.left) queue.push(node.left); // Left child node enqueue
|
||||
if (node.right) queue.push(node.right); // Right child node enqueue
|
||||
let node = queue.shift(); // Dequeue from the queue
|
||||
list.push(node.val); // Store the node value
|
||||
if (node.left) queue.push(node.left); // Enqueue the left child
|
||||
if (node.right) queue.push(node.right); // Enqueue the right child
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
/* Initialize binary tree */
|
||||
// Here we use a function to generate a binary tree directly from an array
|
||||
/* Driver code */
|
||||
/* Initialize the binary tree */
|
||||
// Use a helper function that builds a binary tree directly from an array
|
||||
const root = arrToTree([1, 2, 3, 4, 5, 6, 7]);
|
||||
console.log('\nInitialize binary tree\n');
|
||||
console.log('\nInitialize the binary tree\n');
|
||||
printTree(root);
|
||||
|
||||
/* Level-order traversal */
|
||||
const list = levelOrder(root);
|
||||
console.log('\nLevel-order traversal node print sequence = ' + list);
|
||||
console.log('\nNode sequence from level-order traversal = ' + list);
|
||||
|
||||
Reference in New Issue
Block a user