Merge pull request #783 from Jerry-306/patch-16

新增 0116 填写每一个节点发下一个右侧指针 JavaScript版本 解法代码
This commit is contained in:
程序员Carl
2021-09-29 10:38:13 +08:00
committed by GitHub

View File

@ -1264,7 +1264,41 @@ class Solution:
first = first.left # 从本层扩展到下一层
return root
```
JavaScript:
```javascript
/**
* // 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;
let queue = [root];
while (queue.length) {
let n = queue.length;
for (let i=0; i<n; i++) {
let node = queue.shift();
if (i < n-1) {
node.next = queue[0];
}
node.left && queue.push(node.left);
node.right && queue.push(node.right);
}
}
return root;
};
```
go:
```GO