This is wrong, you must check for curr->left and curr->right and then push them into your queue. Also you are continuing for i != (n-1) case which skips the nodes of non-rightmost tree nodes.
The logic you are trying to apply is basically to see all the nodes in a level order traversal and put the last node of every level.
But you have a given a condition if(i!=n-1) continue; due to this statement your traversal is getting terminated abruptly.
So the correction is just remove that condition and put if(i==n-1) v.push_back(curr->val);
After that when you are pushing nodes in the queue, you are pushing the root->left and root->right which will obviously put this loop in an infinite situation, you need to push curr->left and curr->right instead.
Thanks, pushing root->left and root->right inside the queue is a terrible mistake, and I hope I won’t repeat it again, Thanks a lot for your explanations
Thanks, I thought that we can avoid those nodes which are irrelevant to us in the queue by using the keyword “continue”. But I think, that proved to be a wrong choice in this problem. Thanks a lot for your explanations for the mistake I did in case of queue insertions