var connect = function (root) { let dummyHead = new Node() dummyHead.next = root while (dummyHead.next) { let cur = dummyHead.next let nextHead = new Node() let nextCur = nextHead while (cur) { if (cur.left) { nextCur.next = cur.left nextCur = nextCur.next } if (cur.right) { nextCur.next = cur.right nextCur = nextCur.next } cur = cur.next } dummyHead = nextHead } return root }