hey here i have applied the recursive approach for the problem
your logic to print the sorted array is also correct however the problem with your code is the building of the bst, u have the build the bst in the order provided in the question and not by sorting the array
that is also provided in the following code:
bstnode *insertinbst(bstnode *root, int data)
{
if(root==NULL)
{
return new bstnode(data);
}
if(data<=root->data)
{
root->left = insertinbst(root->left, data);
}
else
root->right = insertinbst(root->right, data);
return root;
}
bstnode *build()
{
int d;
cin>>d;
bstnode *root = NULL;
while(d!=-1)
{
root = insertinbst(root,d);
cin>>d;
}
return root;
}
in main()
{…
bstnode *root = build();
}
use this to build your bst
if your doubt is solved, kindly mark it as resolved