mirror of
https://github.com/krahets/hello-algo.git
synced 2025-07-24 18:55:36 +08:00
Fix remove() in binary search tree.
This commit is contained in:
@ -110,10 +110,15 @@ function remove(num: number): void {
|
||||
// 当子节点数量 = 0 / 1 时, child = null / 该子节点
|
||||
let child = cur.left !== null ? cur.left : cur.right;
|
||||
// 删除节点 cur
|
||||
if (pre!.left === cur) {
|
||||
pre!.left = child;
|
||||
if (cur != root) {
|
||||
if (pre!.left === cur) {
|
||||
pre!.left = child;
|
||||
} else {
|
||||
pre!.right = child;
|
||||
}
|
||||
} else {
|
||||
pre!.right = child;
|
||||
// 若删除节点为根节点,则重新指定根节点
|
||||
root = child;
|
||||
}
|
||||
}
|
||||
// 子节点数量 = 2
|
||||
|
Reference in New Issue
Block a user