my code
#include
using namespace std;
class tnode{
public:
int data;
tnodeleft;
tnoderight;
tnode(int data){
data=data;
left=NULL;
right=NULL;
}
};
tnode* buildbst(int arr[],int s,int d){
if(s>d){
return NULL;
}
int mid=(s+d)/2;
tnode root=new tnode(arr[mid]);
root->left=buildbst(arr,s,mid-1);
root->right=buildbst(arr,mid+1,d);
return root;
}
void preorder(tnode&root){
if(root==NULL){
return;
}
cout<<root->data<<" ";
preorder(root->left);
preorder(root->right);
}
int main() {
int t;
cin>>t;
while(t>0){
int n;
cin>>n;
int arr[100];
for(int i=0;i<n;i++){
cin>>arr[i];
}
tnode* root=buildbst(arr,0,n-1);
preorder(root);
t–;
}
return 0;
}