-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path117.填充每个节点的下一个右侧节点指针-ii.js
More file actions
50 lines (46 loc) · 968 Bytes
/
117.填充每个节点的下一个右侧节点指针-ii.js
File metadata and controls
50 lines (46 loc) · 968 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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/*
* @lc app=leetcode.cn id=117 lang=javascript
*
* [117] 填充每个节点的下一个右侧节点指针 II
*/
// @lc code=start
/**
* // 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 root
}
const queue = [root]
let prev = null
while (queue.length) {
const node = queue.pop()
node.next = prev
if (node.right) {
queue.unshift(node.right)
}
if (node.left) {
queue.unshift(node.left)
}
prev = node
}
function removeRight(node) {
node.next = null
if(node.right) {
removeRight(node.right)
}
}
removeRight(root)
return root
};
// @lc code=end