```
void connect(TreeLinkNode *root) {
if (root == NULL) { return; }
TreeLinkNode* p = root;
TreeLinkNode* nextLevel = root->left;
while (p->left)
{
p->left->next = p->right;
if (p->next)
{
p->right->next = p->next->left;
p = p->next;
}
else
{
p = nextLevel;
nextLevel = p->left;
}
}
}
```