-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy path0116-populating-next-right-pointers-in-each-node.js
50 lines (45 loc) · 1.28 KB
/
0116-populating-next-right-pointers-in-each-node.js
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
/**
* 116. Populating Next Right Pointers in Each Node
* https://leetcode.com/problems/populating-next-right-pointers-in-each-node/
* Difficulty: 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:
*
* 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) {
const order = [];
traverse(order, root);
for (let level of order) {
for (let i = 0; i < level.length - 1; i++) {
level[i].next = level[i + 1];
}
}
return root;
};
function traverse(order, node, level = 0) {
if (!node) {
return [];
}
order[level] = order[level] || [];
order[level].push(node);
traverse(order, node.left, level + 1);
traverse(order, node.right, level + 1);
}