node* Insert(node root, int k)
{
node*s = (node)malloc(sizeof(node));
s->key = k;//把数据传到s中
s->left = NULL;
s->right = NULL;
//为空则作为根节点
if (root == NULL) {
root = s;
}
//大于根节点时
if (k > root->key) {
Insert(root->right,k);
}
//小于根节点时
if (k < root->key) {
Insert(root->left, k);
}
return root;
}
node *createTree()
{
int arr[LEN],i=0;
node*r=NULL;
//用数组存储输入的数字串,-1为结束
for (; i < LEN; i++) {
scanf_s("%d", &arr[i]);
if (arr[i] == -1)
break;
}
//把不等于-1的之前的数据插入tree中
i = 0;
while (arr[i] != -1) {
Insert(r, arr[i]);
i++;
}
return r;
}
问题:为什么插入建立时,每次插入一个key之后,根节点(root)又变为NULL了,调试也看不出什么原因,求大神帮忙解决