快捷导航

二叉树中删除自己要先判断父亲是否为null?

为什么要删除自己还要判断父亲是否为NULL,自己都还没被delete掉,难道父亲会被先delete掉吗?
void Node::Nodedelete(){        if (!this->LeftChild)        {                this->LeftChild->Nodedelete();        }        if (!this->RightChild)        {                this->RightChild->Nodedelete();        }        if (!this->ParentNode)        {                if (this == this->ParentNode->LeftChild)                {                        this->ParentNode->LeftChild = nullptr;                }                if (this == this->ParentNode->RightChild)                {                        this->ParentNode->RightChild = nullptr;                }        }        delete this;}

免责声明:本内容仅代表回答者见解不代表本站观点,请谨慎对待。

版权声明:作者保留权利,不代表本站立场。

回复

使用道具 举报

参与会员1

判断父节点是否为null实际上是判断该节点是否为根节点
回复

使用道具 举报

可能感兴趣的问答

发新帖
  • 微信访问
  • 手机APP