Populate the next pointer for left and right children of the root first (if any of them exists), then use recursive call to process each subtree.

```
void connect(TreeLinkNode *r) {
if (!r) return;
if (r->left) r->left->next = r->right;
if (r->right) r->right->next = (r->next)? r->next->left : NULL;
connect(r->left);
connect(r->right);
}
```