【发布时间】:2015-08-07 10:32:09
【问题描述】:
在二叉搜索树中插入值时给出了分段错误。 这是hackerrank问题所以只有这个方法必须写 这是我写的
typedef struct node
{
int data;
node * left;
node * right;
}node;
node * insert(node * root, int value)
{
node* temp = root;
node* temp1 = new node();
node* prev = NULL ;
while(temp != NULL){
prev= temp;
if(value >= temp->data)
temp = temp->right;
else
temp = temp->left;
}
temp1->data = value;
temp1->left = NULL;
temp1->right = NULL;
if(prev->data >= value)
prev->left = temp1;
else
prev->right = temp1;
return root;
}
我不明白为什么这段代码会给我分段错误。
欢迎所有建议..
【问题讨论】:
-
这应该标记为 C,而不是 C++
-
@kebs 因为
new是一个有效的 c 关键字。 -
@Suraj Palwe:请包括所有相关代码,而不仅仅是一个 sn-p。
-
@Mark Jansen 哎呀,对不起,错过了!我这么说是因为
typedef struct ...有强烈的C 气味。 -
@MarkJansen 这是黑客等级问题,我只需要写这个方法!!
标签: c++ c segmentation-fault binary-search-tree