同层元素的next指针指向它右边的节点
/**
* // Definition for a Node.
* function Node(val,left,right,next) {
* this.val = val;
* this.left = left;
* this.right = right;
* this.next = next;
* };
*/
/**
* @param {Node} root
* @return {Node}
*/
var connect = function (root) {
if (root === null) return null;
let pre = root;
let cur;
while (pre.left) {
cur = pre;
while (cur) {
cur.left.next = cur.right;
if (cur.next) cur.right.next = cur.next.left;
cur = cur.next;
}
pre = pre.left;
}
return root
};