here is my code. after reading T the program reads the first input data and stops working. message "Unhandled exception at 0x00844D30 in tree.exe: 0xC0000005: Access violation reading location 0x00000000." is shown. I included iostream and stddef.h libraries.
class Node{
public:
int data;
Node *left, *right;
Node(int d){
data = d;
left = right = NULL;
}
};
class Solution{
public:
Node* insert(Node* root, int data){
if (root = NULL){
return new Node(data);
}
else{
Node* cur;
if (data <= root->data){
cur = insert(root->left, data);
root->left = cur;
}
else{
cur = insert(root->right, data);
root->right = cur;
}
return root;
}
}
};
int main(){
Solution myTree;
Node* root = NULL;
int T, data;
cin >> T;
while (T-->0){
cin >> data;
root = myTree.insert(root, data);
}
return 0;
}
Node(int d) : data(a), left(nullptr), right(nullptr) {}
would be better – Ed Healif (root = NULL){
should beif (root == NULL){
– Ed Heal