what is wrong with my code it is showing wrong answer
my code:
#include
using namespace std;
class node{
public:
int data;
node* left;
node* right;
node(int d){
data=d;
left=NULL;
right=NULL;
}
};
node* buildBST(int arr,int s,int e){
if(s>e){
return NULL;
}
int mid=(s+e)/2;
node root=new node(arr[mid]);
root->left=buildBST(arr,s,mid-1);
root->right=buildBST(arr,mid+1,e);
return root;
}
void preorder(node* root){
if(root==NULL){
return;
}
cout<data<<" ";
preorder(root->left);
preorder(root->right);
}
int main() {
int t;
cin>>t;
while(t–){
int n;
cin>>n;
int a[n];
for(int i=0;i<n;i++){
cin>>a[i];
}
node* root=buildBST(a,0,n-1);
preorder(root);
}
}
Wrong answer issue
@sawan_verma
Hello Sawan,
please paste your code at coding blocks ide and share the generated code here.
@sawan_verma
Hello sawan,
you code is correct
.
just add cout<<endl; after your preorder function call (because they want output of each test case in different line)
if have any furthur queries then pls ask otherwise mark it as resolved
my code was showing 0 0 0 0 infinite times but i submit it after sometime it passed all the test cases
it was the error of the editor i think.