64,632
社区成员
发帖
与我相关
我的任务
分享
void BSTree::destory()
{
//释放以this为根的左右子树
if(this){
this->LChild->destory();
this->RChild->destory();
delete this;
this = NULL;
}
}
void BSTree::destory(BSTree **pRoot)
{
//释放以pRoot为根的左右子树
if(*pRoot){
destory(&((*pRoot)->LChild));
destory(&((*pRoot)->RChild));
delete *pRoot;
*pRoot = NULL;
}
}
void BSTree::destory()
{
//释放以this为根的左右子树
if(this){
if(this->LChild){
this->LChild->destory();
delete this->LChild;
this->LChild = NULL;
}
if(this->RChild){
this->RChild->destory();
delete this->RChild;
this->RChild = NULL;
}
}
}
void BinaryTree::destroy(TreeNode *&root)
{
if (root->leftChild!=NULL)
destroy(root->leftChild);
if (root->rightChild!=NULL)
destroy(root->rightChild);
delete root;
root = NULL;
}
看来C++下只能按照二楼所说析构函数的方法,再在Main中调用delete pRoot了。