Skip to content

Commit aed0a32

Browse files
committed
Day58
1 parent 4547dbe commit aed0a32

File tree

2 files changed

+60
-1
lines changed

2 files changed

+60
-1
lines changed

Day58/index.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/**
2+
* // Definition for a Node.
3+
* function Node(val, left, right, next) {
4+
* this.val = val === undefined ? null : val;
5+
* this.left = left === undefined ? null : left;
6+
* this.right = right === undefined ? null : right;
7+
* this.next = next === undefined ? null : next;
8+
* };
9+
*/
10+
11+
/**
12+
* @param {Node} root
13+
* @return {Node}
14+
*/
15+
var connect = function (root) {
16+
17+
18+
let queue = [root];
19+
20+
while (queue.length) {
21+
22+
let len = queue.length;
23+
let previous = null;
24+
25+
for (let i = 0; i < len; i++) {
26+
27+
let currentData = queue.shift();
28+
29+
// console.log("Data", currentData.val)
30+
31+
if (!currentData) continue;
32+
33+
if (previous != null) {
34+
35+
//do stuff here
36+
previous.next = currentData;
37+
38+
}
39+
previous = currentData
40+
41+
42+
//left
43+
if (currentData.left) {
44+
queue.push(currentData.left)
45+
}
46+
47+
//right
48+
if (currentData.right) {
49+
queue.push(currentData.right)
50+
}
51+
52+
}
53+
}
54+
return root
55+
56+
57+
};

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,4 +131,6 @@ If you are loving solving problems in leetcode, please contact me to enjoy it to
131131

132132
|Day 56| [382. Linked List Random Node](https://leetcode.com/problems/linked-list-random-node/) | [javascript]()|[:memo:](https://leetcode.com/problems/linked-list-random-node/)|Easy|
133133

134-
|Day 57| [1492. The kth Factor of n](https://leetcode.com/problems/the-kth-factor-of-n/) | [javascript]()|[:memo:](https://leetcode.com/problems/the-kth-factor-of-n/)|Medium|
134+
|Day 57| [1492. The kth Factor of n](https://leetcode.com/problems/the-kth-factor-of-n/) | [javascript]()|[:memo:](https://leetcode.com/problems/the-kth-factor-of-n/)|Medium|
135+
136+
|Day 58| [117. Populating Next Right Pointers in Each Node II](hhttps://leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/) | [javascript]()|[:memo:](https://leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/)|Medium|

0 commit comments

Comments
 (0)