Skip to content

Instantly share code, notes, and snippets.

@AbeEstrada
Created September 2, 2025 02:58
Show Gist options
  • Select an option

  • Save AbeEstrada/61ddf2665b3a72158763962013cd5c1e to your computer and use it in GitHub Desktop.

Select an option

Save AbeEstrada/61ddf2665b3a72158763962013cd5c1e to your computer and use it in GitHub Desktop.
Exercise: Tree Level Order Traversal
// Breadth First Search (BFS)
function levelOrder(root) {
if (!root) return;
const queue = [root];
const result = [];
let index = 0;
while (index < queue.length) {
const currentNode = queue[index++];
result.push(currentNode.data);
if (currentNode.left) queue.push(currentNode.left);
if (currentNode.right) queue.push(currentNode.right);
}
console.log(result.join(" "));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment