this is the code i have written but getting segmentation fault:
#include
using namespace std;
class Node{
public:
int data;
Node * left;
Node * right;
Node(int data){
this->data=data;
this->left=NULL;
this->right=NULL;
}
};
void buildBST(Node * root , int arr[],int index,int n){
if(root==NULL){
Node * newNode = new Node(arr[index]);
index++;
}
if(index==n)
return ;
if(arr[index]data)
buildBST(root->left,arr,index,n);
if(arr[index]>root->data)
buildBST(root->right,arr,index,n);
}
void print(Node * root){
if(root==NULL)
return;
cout<data<<" ";
print(root->left);
print(root->right);
}
int main() {
int t;
cin>>t;
while(t–){
int n;
cin>>n;
int ele[n];
for(int i=0;i<n;i++){
cin>>ele[i];
}
Node * root=NULL;
buildBST(root,ele,0,n);
print(root);
}
return 0;
}