【发布时间】:2021-09-07 06:39:01
【问题描述】:
我正在尝试实现二叉树的插入方法,这就是我得到的:
struct node * addNode(struct node ** root, struct node * node){
//base case: when reach leaf
if (*root == NULL){
printf("reached with root with node key = %i", node->key);
return node;
}
if ((node -> key) > ((*root)->key)){ //add to the right subtree
printf("node key is : %i, tree's node key is: %i\n", (node -> key), ((*root)->key));
(*root)->right = addNode(&((*root)->right), node);
} else {
printf("node key is : %i, tree's node key is: %i\n", (node -> key), ((*root)->key));
(*root)->left = addNode(&((*root)->left), node);
}
return node;
}
在这种情况下,将叶子的子节点设置为 *node 的指针不起作用,但我不知道为什么。写了一个驱动程序来测试这个方法后,我得到了分段错误。
【问题讨论】:
标签: c recursion binary-tree function-definition