mirror of
https://github.com/krahets/hello-algo.git
synced 2026-08-09 18:40:58 +00:00
Fix remove() in binary search tree.
This commit is contained in:
@@ -128,12 +128,18 @@ func (bst *binarySearchTree) remove(num int) {
|
||||
} else {
|
||||
child = cur.Right
|
||||
}
|
||||
// 将子节点替换为待删除节点
|
||||
if pre.Left == cur {
|
||||
pre.Left = child
|
||||
// 删除节点 cur
|
||||
if cur != bst.root {
|
||||
if pre.Left == cur {
|
||||
pre.Left = child
|
||||
} else {
|
||||
pre.Right = child
|
||||
}
|
||||
} else {
|
||||
pre.Right = child
|
||||
// 若删除节点为根节点,则重新指定根节点
|
||||
bst.root = child
|
||||
}
|
||||
|
||||
// 子节点数为 2
|
||||
} else {
|
||||
// 获取中序遍历中待删除节点 cur 的下一个节点
|
||||
|
||||
Reference in New Issue
Block a user