Wrong output for the input
share the input which you are giving.
also as per your code
if (f==NULL){
cout<<endl;
q.pop();
if(!q.empty()){
q.push(NULL);
}
}
this part will never get executed, kyuki hum queue mai null element daal he ni rhe hai
by checking this 2 condition
if (f->left){
q.push(f->left);
}
if (f->right){
q.push(f->right);
}
this is my input 1 2 3 4 5 -1 6 -1 -1 -1 -1 -1 -1
output is 1 2 3 4 5 6
it should be 1 3 6
this is the same input given in hackerblocks but the tree is diiferent please edit my bulidtree code accordingly
hackerblock input is of level order, use this.
node* buildtree(){
int t;
cin>>t;
node*root=new node(t);
queue<node*>q;
q.push(root);
while(!q.empty()){
node*root1=q.front();
q.pop();
int a,b;
cin>>a>>b;
if(a!=-1){
root1->left=new node(a);
q.push(root1->left);
}
if(b!=-1){
root1->right=new node(b);
q.push(root1->right);
}
}
return root;
}
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.