Skip to content

Commit 35824ec

Browse files
committed
✨ [102] GETTING EASIER!
1 parent 170948b commit 35824ec

File tree

3 files changed

+125
-1
lines changed

3 files changed

+125
-1
lines changed

102/my_solution.js

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
class TreeNode {
2+
constructor(val, left, right) {
3+
this.val = (val === undefined ? 0 : val);
4+
this.left = (left === undefined ? null : left);
5+
this.right = (right === undefined ? null : right);
6+
}
7+
}
8+
9+
/**
10+
* @param {TreeNode} root
11+
* @return {number[][]}
12+
*/
13+
const levelOrder = (root) => {
14+
if (null === root) return [];
15+
16+
let list = [[root.val]], children = 2, visitedChildren = 0, nullChildren = 0, level = 1, queue = [root], levelVal = [];
17+
18+
while (queue.length > 0) {
19+
let curr = queue.shift(), left = curr.left, right = curr.right;
20+
console.log(`-> level:${level}, children:${children}, visitedChildren:${visitedChildren}, nullChildren:${nullChildren}, levelVal:${levelVal}`)
21+
console.log(curr)
22+
if (null !== left) {
23+
queue.push(left);
24+
visitedChildren++;
25+
levelVal.push(left.val);
26+
} else {
27+
nullChildren++;
28+
}
29+
30+
if (null !== right) {
31+
queue.push(right);
32+
visitedChildren++;
33+
levelVal.push(right.val);
34+
} else {
35+
nullChildren++;
36+
}
37+
38+
console.log(`--> level:${level}, children:${children}, visitedChildren:${visitedChildren}, nullChildren:${nullChildren}, levelVal:${levelVal}`)
39+
40+
if (children === (visitedChildren + nullChildren)) {
41+
42+
// if (undefined === list[level]) {
43+
if (levelVal.length > 0) list[level] = levelVal;
44+
// } else {
45+
// list[level] = list[level].concat(levelVal)
46+
// }
47+
levelVal = [];
48+
children *= 2;
49+
level++;
50+
visitedChildren = 0;
51+
nullChildren *= 2;
52+
}
53+
}
54+
55+
console.log(list);
56+
57+
return list;
58+
};
59+
60+
// [1,2,null,3,null,4,null,5]
61+
let x =
62+
// levelOrder(
63+
// new TreeNode()
64+
// )
65+
levelOrder(
66+
new TreeNode(1,
67+
new TreeNode(2,
68+
new TreeNode(3,
69+
new TreeNode(4,
70+
new TreeNode(5)
71+
)
72+
)
73+
)
74+
)
75+
)

102/solution.js

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* function TreeNode(val, left, right) {
4+
* this.val = (val===undefined ? 0 : val)
5+
* this.left = (left===undefined ? null : left)
6+
* this.right = (right===undefined ? null : right)
7+
* }
8+
*/
9+
/**
10+
* @param {TreeNode} root
11+
* @return {number[][]}
12+
*/
13+
const levelOrder = (root) => {
14+
if (null === root) return [];
15+
16+
let list = [[root.val]], children = 2, visitedChildren = 0, nullChildren = 0, level = 1, queue = [root], levelVal = [];
17+
18+
while (queue.length > 0) {
19+
let curr = queue.shift(), left = curr.left, right = curr.right;
20+
21+
if (null !== left) {
22+
queue.push(left);
23+
visitedChildren++;
24+
levelVal.push(left.val);
25+
} else {
26+
nullChildren++;
27+
}
28+
29+
if (null !== right) {
30+
queue.push(right);
31+
visitedChildren++;
32+
levelVal.push(right.val);
33+
} else {
34+
nullChildren++;
35+
}
36+
37+
if (children === (visitedChildren + nullChildren)) {
38+
if (levelVal.length > 0) list[level] = levelVal;
39+
levelVal = [];
40+
level++;
41+
children *= 2;
42+
visitedChildren = 0;
43+
nullChildren *= 2;
44+
}
45+
}
46+
47+
return list;
48+
};

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
- [79. Word Search](./79/)
3838
- [83. Remove Duplicates from Sorted List](./83/)
3939
- [100. Same Tree](./100/)
40+
- [102. Binary Tree Level Order Traversal](./102/)
4041
- [104. Maximum Depth of Binary Tree](./104/)
4142
- [121. Best Time to Buy and Sell Stock](./121/)
4243
- [128. Longest Consecutive Sequence](./128/)
@@ -141,7 +142,7 @@ Batch create:
141142
NOTE: JS IS HERE
142143
-->
143144
```ssh
144-
chapter=572 && mkdir ./$chapter && touch ./$chapter/my_solution.js && touch ./$chapter/solution.js && alias x="node ./$chapter/my_solution.js"
145+
chapter=102 && mkdir ./$chapter && touch ./$chapter/my_solution.js && touch ./$chapter/solution.js && alias x="node ./$chapter/my_solution.js"
145146
```
146147
> then you can use `x` for quick debug.
147148

0 commit comments

Comments
 (0)