class Solution {
public:
Node* connect(Node* root) {
if(!root) return root;
auto cur=root;
while(cur){
auto head=new Node(-1);
auto tail=head;
for(auto p=cur;p;p=p->next){
if(p->left) tail=tail->next=p->left;
if(p->right) tail=tail->next=p->right;
}
cur=head->next;
}
return root;
}
};