Skip to content

Problem 102 #26

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ As I work through this list I figure I would make a GitHub repo with my solution
28. [K Closest Points to Origin #973](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/k-closest-origin-973.md)
29. [Longest Substring Without Repeating Characters #3](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/longest-substring-3.md)
30. [3Sum #15](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/3Sum-15.md)
31. Binary Tree Level Order Traversal #102
31. [Binary Tree Level Order Traversal #102](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/binary-tree-level-102.md)
32. Clone Graph #133
33. Evaluate Reverse Polish Notation #150
34. Course Schedule #207
Expand Down Expand Up @@ -112,6 +112,7 @@ In order to practice with similar data structures I'll be placing each problem i
- [Flood Fill #733](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/easy/flood-fill-733.md)
- [Implement Queue using Stacks #232](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/easy/implement-queue-stacks-232.md)
- [Maximum Depth of Binary Tree #104](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/easy/depth-binary-tree-104.md)
- [Binary Tree Level Order Traversal #102](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/binary-tree-level-102.md)

### Stack

Expand Down Expand Up @@ -150,6 +151,7 @@ In order to practice with similar data structures I'll be placing each problem i
- [Balanced Binary Tree #110](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/easy/balanced-binary-tree-110.md)
- [Diameter of Binary Tree #543](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/easy/diameter-binary-tree-543.md)
- [Maximum Depth of Binary Tree #104](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/easy/depth-binary-tree-104.md)
- [Binary Tree Level Order Traversal #102](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/binary-tree-level-102.md)

### Heap

Expand Down Expand Up @@ -183,6 +185,7 @@ Within the problems above there are several patterns that often occur. I plan to
- [Flood Fill #733](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/easy/flood-fill-733.md)
- [Maximum Depth of Binary Tree #104](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/easy/depth-binary-tree-104.md)
- [01 Matrix #542](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/01-matrix-542.md)
- [Binary Tree Level Order Traversal #102](https://github.com/curtisbarnard/leetcode-grind75-javascript/blob/main/medium/binary-tree-level-102.md)

### Depth First Search

Expand Down
105 changes: 105 additions & 0 deletions medium/binary-tree-level-102.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
# Binary Tree Level Order Traversal

Page on leetcode: https://leetcode.com/problems/binary-tree-level-order-traversal/

## Problem Statement

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).

### Constraints

- The number of nodes in the tree is in the range [0, 2000].
- -1000 <= Node.val <= 1000

### Example

```
Input: root = [3,9,20,null,null,15,7]
Output: [[3],[9,20],[15,7]]
```

## Solution

- BFS with a queue
- Only need to return the values
- Need to track the level so that once a level is done a new array is created
- Create a result array

### Pseudocode

1. Create a result array
2. Create a queue
3. Add root to queue with level 1
4. Loop while queue is not empty
5. Loop on level
6. Dequeue and push value to level array
7. If node.left is valid push to queue, same with right with level as curlevel + 1
8. Once level is done push level array to result array
9. Increment level
10. Return result

### Initial Solution

This solution has a time and space complexity O(n).

```javascript
const levelOrder = function (root) {
const result = [];
// Handle if root is null
if (root) {
const queue = [[root, 1]];
let level = 1;
while (queue.length > 0) {
const levelArr = [];
// Looping on current level
while (queue.length > 0 && level === queue[0][1]) {
const node = queue.shift();
levelArr.push(node[0].val);
if (node[0].left) {
queue.push([node[0].left, node[1] + 1]);
}
if (node[0].right) {
queue.push([node[0].right, node[1] + 1]);
}
}
// Add everything from current level to result
result.push(levelArr);
level++;
}
}
return result;
};
```

### Optimized Solution

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

```javascript
const levelOrder = function (root) {
const result = [];
// Handle if root is null
if (root) {
const queue = [root];
while (queue.length > 0) {
const qLen = queue.length;
const levelArr = [];
// Looping on current level
for (let i = 0; i < qLen; i++) {
const node = queue.shift();
// queue will have null nodes. If nodes are null we just skip them.
if (node) {
levelArr.push(node.val);
queue.push(node.left);
queue.push(node.right);
}
}
// Add everything from current level to result is it is non empty
if (levelArr.length > 0) {
result.push(levelArr);
}
}
}
return result;
};
```