```
class Solution {
public:
void connect(TreeLinkNode *root) {
if (!root)
return;
TreeLinkNode *leftMost, *cur, *rightMost;
leftMost = root;
while (leftMost) {
cur = leftMost;
leftMost = rightMost = nullptr;
while (cur) {
for (TreeLinkNode* i: {cur->left, cur->right}) {
if (!i)
continue;
if (!leftMost) {
leftMost = rightMost = i;
continue;
}
rightMost->next = i;
rightMost = i;
}
cur = cur->next;
}
}
}
};
```

Leftmost indicates the the leftmost node in current level.