我遇到了这个清除二叉搜索树的方法。代码在教科书中给出。为什么最后一定要创建和删除一个节点temp?为什么不删除子根而不是将其设为空?
void Binary_tree<Entry> :: recursive_clear(Binary_node<Entry> * &sub_root)
/* Post: The subtree rooted at sub_root is cleared. */
{
Binary_node<Entry> *temp = sub_root;
if (sub_root == NULL) return;
recursive_clear(sub_root->left);
recursive_clear(sub_root->right);
sub_root = NULL;
delete temp;
}
最佳答案
那是因为删除动态内存的标准方法是使用 delete 和 delete it's used with pointers .
关于c++ - 清除二叉搜索树,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16255273/