Free memory after removing

a node from a LinkedList or TreeNode.
This commit is contained in:
Yudong Jin
2023-01-02 19:53:55 +08:00
parent 6b02449f22
commit 410c5d6b62
20 changed files with 69 additions and 140 deletions

View File

@@ -101,7 +101,7 @@ class BinarySearchTree {
// 子结点数量 = 2
else {
// 获取中序遍历中 cur 的下一个结点
TreeNode nex = min(cur.right);
TreeNode nex = getInOrderNext(cur.right);
int tmp = nex.val;
// 递归删除结点 nex
remove(nex.val);
@@ -111,8 +111,8 @@ class BinarySearchTree {
return cur;
}
/* 获取最小结点 */
public TreeNode min(TreeNode root) {
/* 获取中序遍历中的下一个结点(仅适用于 root 有左子结点的情况) */
public TreeNode getInOrderNext(TreeNode root) {
if (root == null) return root;
// 循环访问左子结点,直到叶结点时为最小结点,跳出
while (root.left != null) {