-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path116.js
More file actions
34 lines (29 loc) · 768 Bytes
/
116.js
File metadata and controls
34 lines (29 loc) · 768 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/**
* Definition for binary tree with next pointer.
* function TreeLinkNode(val) {
* this.val = val;
* this.left = this.right = this.next = null;
* }
*/
/**
* @param {TreeLinkNode} root
* @return {void} Do not return anything, modify tree in-place instead.
*/
var connect = function(root) {
if (root === null) return;
let currentList = [root];
let nextList = [];
while(currentList.length > 0) {
for(var i = 0; i < currentList.length; ++i) {
if (i !== currentList.length - 1) {
currentList[i].next = currentList[i + 1];
}
}
currentList.forEach(node => {
node.left && nextList.push(node.left);
node.right && nextList.push(node.right);
});
currentList = nextList;
nextList = [];
}
};