-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpopulating-next-pointers-in-each-node.js
More file actions
58 lines (44 loc) · 1.46 KB
/
populating-next-pointers-in-each-node.js
File metadata and controls
58 lines (44 loc) · 1.46 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
//https://leetcode.com/problems/populating-next-right-pointers-in-each-node/
// 116. Populating Next Right Pointers in Each Node
// Medium
// You are given a perfect binary tree where all leaves are on the same level, and every parent has two children. The binary tree has the following definition:
// struct Node {
// int val;
// Node *left;
// Node *right;
// Node *next;
// }
// Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to NULL.
// Initially, all next pointers are set to NULL.
//
/**
* // Definition for a Node.
* function Node(val, left, right, next) {
* this.val = val === undefined ? null : val;
* this.left = left === undefined ? null : left;
* this.right = right === undefined ? null : right;
* this.next = next === undefined ? null : next;
* };
*/
/**
* @param {Node} root
* @return {Node}
*/
var connect = function(root) {
if (!root) return null
let currentLevel = [root];
let nextLevel = [];
while (currentLevel.length > 0 ){
let i = 0
while( i < currentLevel.length) {
if (currentLevel[i+1]) { currentLevel[i].next = currentLevel[i+1] }
const curNode = currentLevel[i];
i++
if (curNode.left) { nextLevel.push(curNode.left) }
if (curNode.right) { nextLevel.push(curNode.right) }
}
currentLevel = nextLevel;
nextLevel = []
}
return root
};