|
| 1 | +# Binary Tree Level Order Traversal |
| 2 | + |
| 3 | +Page on leetcode: https://leetcode.com/problems/binary-tree-level-order-traversal/ |
| 4 | + |
| 5 | +## Problem Statement |
| 6 | + |
| 7 | +Given the root of a binary tree, return the level order traversal of its nodes' values. (i.e., from left to right, level by level). |
| 8 | + |
| 9 | +### Constraints |
| 10 | + |
| 11 | +- The number of nodes in the tree is in the range [0, 2000]. |
| 12 | +- -1000 <= Node.val <= 1000 |
| 13 | + |
| 14 | +### Example |
| 15 | + |
| 16 | +``` |
| 17 | +Input: root = [3,9,20,null,null,15,7] |
| 18 | +Output: [[3],[9,20],[15,7]] |
| 19 | +``` |
| 20 | + |
| 21 | +## Solution |
| 22 | + |
| 23 | +- BFS with a queue |
| 24 | +- Only need to return the values |
| 25 | +- Need to track the level so that once a level is done a new array is created |
| 26 | +- Create a result array |
| 27 | + |
| 28 | +### Pseudocode |
| 29 | + |
| 30 | +1. Create a result array |
| 31 | +2. Create a queue |
| 32 | +3. Add root to queue with level 1 |
| 33 | +4. Loop while queue is not empty |
| 34 | +5. Loop on level |
| 35 | +6. Dequeue and push value to level array |
| 36 | +7. If node.left is valid push to queue, same with right with level as curlevel + 1 |
| 37 | +8. Once level is done push level array to result array |
| 38 | +9. Increment level |
| 39 | +10. Return result |
| 40 | + |
| 41 | +### Initial Solution |
| 42 | + |
| 43 | +This solution has a time and space complexity O(n). |
| 44 | + |
| 45 | +```javascript |
| 46 | +const levelOrder = function (root) { |
| 47 | + const result = []; |
| 48 | + // Handle if root is null |
| 49 | + if (root) { |
| 50 | + const queue = [[root, 1]]; |
| 51 | + let level = 1; |
| 52 | + while (queue.length > 0) { |
| 53 | + const levelArr = []; |
| 54 | + // Looping on current level |
| 55 | + while (queue.length > 0 && level === queue[0][1]) { |
| 56 | + const node = queue.shift(); |
| 57 | + levelArr.push(node[0].val); |
| 58 | + if (node[0].left) { |
| 59 | + queue.push([node[0].left, node[1] + 1]); |
| 60 | + } |
| 61 | + if (node[0].right) { |
| 62 | + queue.push([node[0].right, node[1] + 1]); |
| 63 | + } |
| 64 | + } |
| 65 | + // Add everything from current level to result |
| 66 | + result.push(levelArr); |
| 67 | + level++; |
| 68 | + } |
| 69 | + } |
| 70 | + return result; |
| 71 | +}; |
| 72 | +``` |
| 73 | + |
| 74 | +### Optimized Solution |
| 75 | + |
| 76 | +This solution has a time and space complexity O(n). It's pretty much the same as above just using a cleaner method to track each level. You can see an explanation of the approach here: https://www.youtube.com/watch?v=6ZnyEApgFYg |
| 77 | + |
| 78 | +```javascript |
| 79 | +const levelOrder = function (root) { |
| 80 | + const result = []; |
| 81 | + // Handle if root is null |
| 82 | + if (root) { |
| 83 | + const queue = [root]; |
| 84 | + while (queue.length > 0) { |
| 85 | + const qLen = queue.length; |
| 86 | + const levelArr = []; |
| 87 | + // Looping on current level |
| 88 | + for (let i = 0; i < qLen; i++) { |
| 89 | + const node = queue.shift(); |
| 90 | + // queue will have null nodes. If nodes are null we just skip them. |
| 91 | + if (node) { |
| 92 | + levelArr.push(node.val); |
| 93 | + queue.push(node.left); |
| 94 | + queue.push(node.right); |
| 95 | + } |
| 96 | + } |
| 97 | + // Add everything from current level to result is it is non empty |
| 98 | + if (levelArr.length > 0) { |
| 99 | + result.push(levelArr); |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + return result; |
| 104 | +}; |
| 105 | +``` |
0 commit comments