-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy path0117-populating-next-right-pointers-in-each-node-ii.js
More file actions
49 lines (44 loc) · 1.19 KB
/
0117-populating-next-right-pointers-in-each-node-ii.js
File metadata and controls
49 lines (44 loc) · 1.19 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
/**
* 117. Populating Next Right Pointers in Each Node II
* https://leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/
* Difficulty: Medium
*
* Given a binary tree, 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 === null) return root;
const depth = 0;
const stack = [[root, depth]];
while (stack.length) {
const [node, depth] = stack.shift();
if (stack.length) {
const [nextNode, nextDepth] = stack[0];
if (depth === nextDepth) {
node.next = nextNode;
}
}
if (node.left) {
stack.push([node.left, depth + 1]);
}
if (node.right) {
stack.push([node.right, depth + 1]);
}
}
return root;
};