```
class Solution {
public:
void connect(TreeLinkNode *root) {
helper(root);
TreeLinkNode *p=root;
while(p){
p->next = nullptr;
p = p->right;
}
}
void helper(TreeLinkNode *root){
if(!root||(!root->left&&!root->right)) return;
TreeLinkNode *p = root->left, *q = root->right;
while(p&&q){
p->next = q;
p = p->right;
q = q->left;
}
helper(root->left);
helper(root->right);
}
};
```