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