dimanche 2 juillet 2017

Taking binary tree as input(recursively)

I am supposed to take input of a binary tree recursively as :

10 true 20 true 40 false false true 50 false false true 30 true 60 false false true 73 false false

so the tree created should be:-

        10
    20      30 
 40   50  60   73

I have created a function which cannot create this tree, whats wrong in the code?

node* takeInput(){
int data;
string a;

cin>>data>>a;
node*n = NULL;

if(a=="true"){
    n = new node(data);
    n->left = takeInput();
    n->right = takeInput();
}
if(a=="false"){
   return NULL;
}
return n;

}

Aucun commentaire:

Enregistrer un commentaire