Print bst in given range


why code is giving wrong output

Dakshi… The approach you are using for building the tree is not correct… Pls use the approach as,

node* insert(node* root,int data)
{
if(root==NULL)
{
//return newB(d);
return new node(data);
}
if(data<=root->data)
{
root->left=insert(root->left,data);
}
else
{
root->right=insert(root->right,data);
}
return root;
}

void buildtree(node *&root) // function to build the tree
{
int n;
cin>>n;
int data;
while(n>0)
{
cin>>data;
root=insert(root,data);
n–;
}
}