Add destructors to the C++ codes.

This commit is contained in:
Yudong Jin
2023-01-14 19:52:11 +08:00
parent 87acfc91ab
commit bb657f9517
19 changed files with 121 additions and 24 deletions

View File

@ -17,6 +17,10 @@ public:
root = buildTree(nums, 0, nums.size() - 1); // 构建二叉搜索树
}
~BinarySearchTree() {
freeMemoryTree(root);
}
/* 获取二叉树根结点 */
TreeNode* getRoot() {
return root;
@ -152,5 +156,8 @@ int main() {
cout << endl << "删除结点 4 后,二叉树为\n" << endl;
PrintUtil::printTree(bst->getRoot());
// 释放内存
delete bst;
return 0;
}