We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
I think I can explain.
First the push() function insert always at the end of an array.
Second the pop() function delete the first element in an array.
For the program of anvitakamat:
// Basically he creates an arrayqueue<node*>q;// q = []// ...then a pointer to the root treenode*temp=root;//insert the root value into the arrayq.push(root);// q = [root]// We arrive to the while loop and q is not emptywhile(!q.empty()){// ... now we are in...}
Now is the tricky part...
cout<<temp->data<<" ";// it print the root's data value since temp points to rootif(temp->left!=NULL)// if left branch availableq.push(temp->left);// ... it stacks left branch// q = [root, root->left]if(temp->right!=NULL)// if right branch availableq.push(temp->right);// ... it stacks right branch// q = [root, root->left, root->right]q.pop();// q = [root->left, root->right]temp=q.front();// temp points now to root->left// and it starts over again// and it will stacks all left and right sides but print one data at a time
Hope it's been helpful.
If you have more questions go on.
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Tree: Level Order Traversal
You are viewing a single comment's thread. Return to all comments →
I think I can explain. First the push() function insert always at the end of an array. Second the pop() function delete the first element in an array.
For the program of anvitakamat:
Now is the tricky part...
Hope it's been helpful. If you have more questions go on.