Nt getting any output
hey @Saransh99, the way reading data in not according to input format given in question. I have made changes to your code you can check them here.
no its nt givin’ the correct output but the input itself
the output is not correct .
hey @Saransh99, with a given array multiple BST are posible and the output you getting in above code is also a valid BST. But read the question carefully they asking to build balanced BST.
balanced BST- if the heights of its subtrees differ by no more than 1. (That is, if the subtrees have heights h1 and h2, then |h1 − h2| ≤ 1.)
In that case we have to break the tree from mid of the array.
here is the logic to build such tree
node* build(int arr,node root,int s,int e)
{
if(s>e)
{
return NULL;
}
int mid=(s+e)/2;
root=new node(arr[mid]);
root->left=build(arr,root->left,s,mid-1);
root->right=build(arr,root->right,mid+1,e);
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.