Skip to content

Commit d1b7098

Browse files
authored
Create 117-populating-next-right-pointers-in-each-node-ii.js
1 parent 801cc98 commit d1b7098

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* // Definition for a Node.
3+
* function Node(val,left,right,next) {
4+
* this.val = val;
5+
* this.left = left;
6+
* this.right = right;
7+
* this.next = next;
8+
* };
9+
*/
10+
/**
11+
* @param {Node} root
12+
* @return {Node}
13+
*/
14+
const connect = function(root) {
15+
if (root == null) return null
16+
const cur = [root]
17+
while (cur.length) {
18+
const len = cur.length
19+
for (let i = 0; i < len; i++) {
20+
const el = cur.shift()
21+
if (i === len - 1) el.next = null
22+
else el.next = cur[0]
23+
if (el.left) cur.push(el.left)
24+
if (el.right) cur.push(el.right)
25+
}
26+
}
27+
return root
28+
}

0 commit comments

Comments
 (0)