how to give level order input with explaination and solution
Level order input for the binary tree
online ide is not working hope this code is readable to you if not tell me ill share somewhere else
`
// this is same as bfs
Node* buildTree(){
queue<Node*>q;
int d;cin>>d;
// if -1 return null
if(d==-1)return NULL;
// else make node and push to queue
Node*head=new Node(d);
q.push(head);
int lc,rc;
// now till the queue is not empty pop and push the children if next input is not -1
while(!q.empty()){
Node*f=q.front();
q.pop();
cin>>lc>>rc;
if(lc!=-1){
f->left=new Node(lc);
q.push(f->left);
}
if(rc!=-1){
f->right=new Node(rc);
q.push(f->right);
}
}
// finally return head
return head;
}`
hi @akki56756_bab14f919dbad123, check here https://ideone.com/QC0NUL
the CB ide is not working so sharing on ideone, hope the code is visible
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.