class Solution {

public:

void connect(TreeLinkNode *root) {

vector<TreeLinkNode * > a,b;

int i,j;

if(root == NULL)

return;

a.push_back(root);

root->next = NULL;

while(1)

{

for(i = 0;i < a.size();i++)

{

if(a[i]->left != NULL)

b.push_back(a[i]->left);

if(a[i]->right != NULL)

b.push_back(a[i]->right);

for(j = 0;j < b.size() - 1;j++)

b[j]->next = b[j + 1];

if(b.size() > 0)

b[j]->next = NULL;

a.clear();

a = b;

b.clear();

}

if(a.empty())

break;

}

```
}
```

};