heyy in this code how can i build tree from the given output . I guess my tree is not building in the required way thats why i m getting wrong output. Please check,
Wrong output............:(
@Aparna
yes you are building tree in wrong way. Rest of your code is correct. You can use queue to build tree. I am adding part of code for building tree, check it out
node *buildtree(){
int d;
cin >> d;
node *root = new node(d);
queue<node*> q;
q.push(root);
while(!q.empty()){
node *f = q.front();
q.pop();
int l, r;
cin >> l >> r;
if(l != -1){
f->left = new node(l);
q.push(f->left);
}
if(r != -1){
f->right = new node(r);
q.push(f->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.