Level order traversal approach


#1

void Solution::connect(TreeLinkNode* root) {
if(!root) return;
queue<TreeLinkNode*> q;
q.push(root);
while(1){
int nodecount=q.size();
if(nodecount==0) break;
while(nodecount>0){
TreeLinkNode* temp=q.front();
q.pop();
if(nodecount!=1) temp->next=q.front();
if(temp->left) q.push(temp->left);
if(temp->right) q.push(temp->right);
nodecount–;
}
}
return;
}