Getting the Error /bin/run.sh: line 4: 21 Segmentation fault (core dumped) ./exe
on this simple code. My input is 5 4 2 6 7 -1
#include <iostream>
using namespace std;
class node {
public:
int data;
node*left; node*right;
node(int d){
data = d;
left = NULL;
right = NULL;
}
};
node* insertInBST(node *root, int data){
if(root = NULL){
return new node(data);
}
if(data <= root->data){
root->left = insertInBST(root->left,data);
} else {
root->right = insertInBST(root->right,data);
}
return root;
}
node* buildTree(){
int d;
cin>>d;
node*root = NULL;
while(d!=-1){
root = insertInBST(root,d);
cin>>d;
}
return root;
}
// inOrder
void printIn(node *a){
if(a == NULL){
return;
}
printIn(a->left);
cout<<a->data<<" ";
printIn(a->right);
}
int main()
{
node *BST = buildTree();
printIn(BST);
return 0;
}