This commit is contained in:
krahets
2023-10-08 01:43:28 +08:00
parent 3d2d669b43
commit baac2d11a7
52 changed files with 999 additions and 625 deletions
+6 -6
View File
@@ -111,7 +111,7 @@ comments: true
```csharp title="binary_search_tree.cs"
/* 查找节点 */
TreeNode? search(int num) {
TreeNode? Search(int num) {
TreeNode? cur = root;
// 循环查找,越过叶节点后跳出
while (cur != null) {
@@ -434,7 +434,7 @@ comments: true
```csharp title="binary_search_tree.cs"
/* 插入节点 */
void insert(int num) {
void Insert(int num) {
// 若树为空,则初始化根节点
if (root == null) {
root = new TreeNode(num);
@@ -456,7 +456,7 @@ comments: true
}
// 插入节点
TreeNode node = new TreeNode(num);
TreeNode node = new(num);
if (pre != null) {
if (pre.val < num)
pre.right = node;
@@ -953,7 +953,7 @@ comments: true
```csharp title="binary_search_tree.cs"
/* 删除节点 */
void remove(int num) {
void Remove(int num) {
// 若树为空,直接提前返回
if (root == null)
return;
@@ -977,7 +977,7 @@ comments: true
// 子节点数量 = 0 or 1
if (cur.left == null || cur.right == null) {
// 当子节点数量 = 0 / 1 时, child = null / 该子节点
TreeNode? child = cur.left != null ? cur.left : cur.right;
TreeNode? child = cur.left ?? cur.right;
// 删除节点 cur
if (cur != root) {
if (pre.left == cur)
@@ -997,7 +997,7 @@ comments: true
tmp = tmp.left;
}
// 递归删除节点 tmp
remove(tmp.val);
Remove(tmp.val);
// 用 tmp 覆盖 cur
cur.val = tmp.val;
}