/**
* Definition for binary tree with next pointer.
* struct TreeLinkNode {
* int val;
* TreeLinkNode *left, *right, *next;
* TreeLinkNode(int x) : val(x), left(NULL), right(NULL), next(NULL) {}
* };
*/
class Solution {
public:
void connect(TreeLinkNode *root) {
if(root == NULL)
return;
queue<TreeLinkNode*> q;
q.push(root);
q.push(NULL);
while(q.size() > 1)
{
TreeLinkNode* t = q.front();
q.pop();
if(t == NULL)
{
q.push(NULL);
continue;
}
t->next = q.front();
if(t->left)
q.push(t->left);
if(t->right)
q.push(t->right);
}
}
};