有人介意简要解释为什么我不断从此代码中获取分段错误吗?

“目标”是要删除的节点,我正在尝试将其替换为左侧分支上最右侧的叶子。 “父母”是目标的父母。

    else if (target->left != NULL && target->right != NULL) {
      Node * childishDad = target;
      Node * childish = target->left;

      while(childish->right != NULL){
        childishDad = childish;
        childish = childish->right;
      }
      if(parent->left == target)
        parent->left = childish;

      if(parent->right == target)
        parent->right = childish;

      childish->left = target->left;
      childish->right = target->right;

      if(childishDad != target)
        childishDad->right = NULL;

      delete target;
      return true;
    }

最佳答案

当while循环完成时,您知道

childish->right == NULL


但是,稍后分配

childish->right = target->right;


也许这就是问题所在。

关于c++ - 删除有两个 child 的目标,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28310564/

10-11 18:52