mirror of
https://github.com/krahets/hello-algo.git
synced 2026-08-10 19:10:57 +00:00
Free memory after removing
a node from a LinkedList or TreeNode.
This commit is contained in:
@@ -120,7 +120,7 @@ function remove(num: number): TreeNode | null {
|
||||
// 子结点数量 = 2
|
||||
else {
|
||||
// 获取中序遍历中 cur 的下一个结点
|
||||
let next = min(cur.right);
|
||||
let next = getInOrderNext(cur.right);
|
||||
let tmp = next!.val;
|
||||
// 递归删除结点 nex
|
||||
remove(next!.val);
|
||||
@@ -130,8 +130,8 @@ function remove(num: number): TreeNode | null {
|
||||
return cur;
|
||||
}
|
||||
|
||||
/* 获取最小结点 */
|
||||
function min(root: TreeNode | null): TreeNode | null {
|
||||
/* 获取中序遍历中的下一个结点(仅适用于 root 有左子结点的情况) */
|
||||
function getInOrderNext(root: TreeNode | null): TreeNode | null {
|
||||
if (root === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user