Easy C++ solution using BFS


#1

void Solution::connect(TreeLinkNode* A)
{
if(A==NULL)
{
return;
}
queue<TreeLinkNode *>q;
q.push(A);
while(!q.empty())
{
int count=q.size();
while(count)
{
TreeLinkNode *tmp1=q.front();
q.pop();
TreeLinkNode *tmp2;
if(q.empty() || count==1)
{
tmp2=NULL;
}
else
{
tmp2=q.front();
}
tmp1->next=tmp2;
if(tmp1->left)
{
q.push(tmp1->left);
}
if(tmp1->right)
{
q.push(tmp1->right);
}
count–;
}
}
}


#2

Elegant code. Thanks mate.


#3

why did you put count==1 ??