Created
September 2, 2025 02:58
-
-
Save AbeEstrada/61ddf2665b3a72158763962013cd5c1e to your computer and use it in GitHub Desktop.
Exercise: Tree Level Order Traversal
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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