mirror of
https://github.com/krahets/hello-algo.git
synced 2025-11-02 12:58:42 +08:00
Fine tune
This commit is contained in:
@ -660,7 +660,6 @@ AVL 树的独特之处在于「旋转 Rotation」的操作,其可 **在不影
|
||||
// 返回子树的根节点
|
||||
return node;
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
### 删除结点
|
||||
@ -765,7 +764,7 @@ AVL 树的独特之处在于「旋转 Rotation」的操作,其可 **在不影
|
||||
}
|
||||
|
||||
/* 递归删除结点(辅助函数) */
|
||||
private TreeNode? removeHelper(TreeNode? node, int? val)
|
||||
private TreeNode? removeHelper(TreeNode? node, int val)
|
||||
{
|
||||
if (node == null) return null;
|
||||
/* 1. 查找结点,并删除之 */
|
||||
@ -789,8 +788,8 @@ AVL 树的独特之处在于「旋转 Rotation」的操作,其可 **在不影
|
||||
{
|
||||
// 子结点数量 = 2 ,则将中序遍历的下个结点删除,并用该结点替换当前结点
|
||||
TreeNode? temp = minNode(node.right);
|
||||
node.right = removeHelper(node.right, temp?.val);
|
||||
node.val = temp?.val;
|
||||
node.right = removeHelper(node.right, temp.val);
|
||||
node.val = temp.val;
|
||||
}
|
||||
}
|
||||
updateHeight(node); // 更新结点高度
|
||||
|
||||
@ -692,7 +692,7 @@ comments: true
|
||||
|
||||
```csharp title="binary_search_tree.cs"
|
||||
/* 删除结点 */
|
||||
TreeNode? remove(int? num)
|
||||
TreeNode? remove(int num)
|
||||
{
|
||||
// 若树为空,直接提前返回
|
||||
if (root == null) return null;
|
||||
@ -724,7 +724,6 @@ comments: true
|
||||
{
|
||||
pre.right = child;
|
||||
}
|
||||
|
||||
}
|
||||
// 子结点数量 = 2
|
||||
else
|
||||
@ -733,7 +732,7 @@ comments: true
|
||||
TreeNode? nex = min(cur.right);
|
||||
if (nex != null)
|
||||
{
|
||||
int? tmp = nex.val;
|
||||
int tmp = nex.val;
|
||||
// 递归删除结点 nex
|
||||
remove(nex.val);
|
||||
// 将 nex 的值复制给 cur
|
||||
@ -742,7 +741,7 @@ comments: true
|
||||
}
|
||||
return cur;
|
||||
}
|
||||
|
||||
|
||||
/* 获取最小结点 */
|
||||
TreeNode? min(TreeNode? root)
|
||||
{
|
||||
|
||||
@ -156,11 +156,11 @@ comments: true
|
||||
Queue<TreeNode> queue = new();
|
||||
queue.Enqueue(root);
|
||||
// 初始化一个列表,用于保存遍历序列
|
||||
List<int?> list = new();
|
||||
List<int> list = new();
|
||||
while (queue.Count != 0)
|
||||
{
|
||||
TreeNode node = queue.Dequeue(); // 队列出队
|
||||
list.Add(node.val); // 保存结点值
|
||||
TreeNode node = queue.Dequeue(); // 队列出队
|
||||
list.Add(node.val); // 保存结点值
|
||||
if (node.left != null)
|
||||
queue.Enqueue(node.left); // 左子结点入队
|
||||
if (node.right != null)
|
||||
|
||||
Reference in New Issue
Block a user