Merge branch 'master' into binary_search_tree

This commit is contained in:
Yudong Jin
2023-01-10 13:30:38 +08:00
committed by GitHub
215 changed files with 5618 additions and 1642 deletions

View File

@@ -1,4 +1,4 @@
/*
/**
* File: binary_search_tree.cpp
* Created Time: 2022-11-25
* Author: Krahets (krahets@163.com)
@@ -96,11 +96,13 @@ public:
// 删除结点 cur
if (pre->left == cur) pre->left = child;
else pre->right = child;
// 释放内存
delete cur;
}
// 子结点数量 = 2
else {
// 获取中序遍历中 cur 的下一个结点
TreeNode* nex = min(cur->right);
TreeNode* nex = getInOrderNext(cur->right);
int tmp = nex->val;
// 递归删除结点 nex
remove(nex->val);
@@ -110,8 +112,8 @@ public:
return cur;
}
/* 获取最小结点 */
TreeNode* min(TreeNode* root) {
/* 获取中序遍历中的下一个结点(仅适用于 root 有左子结点的情况) */
TreeNode* getInOrderNext(TreeNode* root) {
if (root == nullptr) return root;
// 循环访问左子结点,直到叶结点时为最小结点,跳出
while (root->left != nullptr) {