input:-
1
10
1 2 3 4 5 6 7 8 9 10
please tell me how to debug this program?
Code is not giving correct result
Just made little changes to it . Please check them if it is working correct now.
#include<iostream>
using namespace std;
class node{
public:
int data;
node* left;
node* right;
node(int d)
{
data=d;
left=nullptr;
right=nullptr;
}
};
node* buildBalancedBST(int a[],int i,int j)
{
// cout<<i<<" "<<j<<endl;
if(i==j)
{
return new node(a[i]);
}
else if(i>j)
return NULL;
else
{
int mid=(i+j)/2;
node* left=buildBalancedBST(a,i,mid-1);
node* right=buildBalancedBST(a,mid+1,j);
node* nd=new node(a[mid]);
nd->left=left;
nd->right=right;
return nd;
}
// return nullptr;
}
void printPreorder(node* root)
{
if(root)
{
cout<<root->data<<" ";
printPreorder(root->left);
printPreorder(root->right);
}
}
int main() {
int t;
cin>>t;
while(t--)
{
int a[10000];
int n;
cin>>n;
for(int i=0;i<n;i++)
cin>>a[i];
node* root=buildBalancedBST(a,0,n-1);
printPreorder(root);
}
}
Its giving wrong answer