i am unable to understand how should i take input with true and false with the node values
How to take input in this case
Basically, here true means having a child and false means having no child…So for the given input :
10 true 20 true 40 false false true 50 false false true 30 true 60 false false true 73 false false
It means that 10 is the root node, true means it is having left child whose value is 20, further true means that 20 is having a left child as 40, now two times false means that 40 doesnt have left or right child, further true means that 50 is the right child of 20… and two times false means that it doesnt have any left or right child. …This way the tree will be built.
Now for the code part, you can basically take a global variable as x=0, and a string str, and then :
if(x==0)
{
x++;
int d;
cin>>d;
root=new node(d);
root->left=buildTree(root->left);
root->right=buildTree(root->right);
return root;
}
else
{
string str;
cin>>str;
if(str==“true”)
{
int k;
cin>>k;
node *temp=new node(k);
temp->left=buildTree(temp->left);
temp->right=buildTree(temp->right);
return temp;
}
else
if(str==“false”)
{
return root;
}
}
return root;
}