Skip to content

Commit 96bba74

Browse files
Problem 102 (#26)
* add solution and resources * update readme
1 parent c7b335e commit 96bba74

File tree

2 files changed

+109
-1
lines changed

2 files changed

+109
-1
lines changed

README.md

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

116117
### Stack
117118

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

154156
### Heap
155157

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

187190
### Depth First Search
188191

medium/binary-tree-level-102.md

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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

Comments
 (0)