小编use*_*796的帖子

访问二叉树左节点的指针时出现 SIGSEGV,即使该指针已初始化

我正在尝试创建一个返回二叉树的镜像副本的函数。我所说的“镜像”是指一棵树,每个左节点作为其右节点,反之亦然。

我进行此练习的页面中的视觉示例。

The one on the left gets copied to resemble the one on the right. This is the code of the function, with the definition of the binary nodes and "insert node" function that I use:

typedef struct bNode {
    int data;
    struct bNode *left;
    struct bNode *right;
} bNode;
    
//  =============================================================
    
bNode* reverse_tree (bNode **tree) {
    bNode *copy = malloc(sizeof(bNode));
    copy->data = (*tree)->data;
    if (!((*tree)->right) && !((*tree)->left)){
        return copy;
    }
        
    copy->left = reverse_tree(&(*tree)->right);
    copy->right = reverse_tree(&(*tree)->left);
    return copy;
} …
Run Code Online (Sandbox Code Playgroud)

c recursion reverse binary-tree function-definition

4
推荐指数
1
解决办法
46
查看次数

标签 统计

binary-tree ×1

c ×1

function-definition ×1

recursion ×1

reverse ×1