This commit is contained in:
krahets
2023-07-26 15:34:46 +08:00
parent f8f7086196
commit 7351ec70a6
53 changed files with 439 additions and 329 deletions
+12 -12
View File
@@ -111,10 +111,10 @@ comments: true
node := bst.root
// 循环查找,越过叶节点后跳出
for node != nil {
if node.Val < num {
if node.Val.(int) < num {
// 目标节点在 cur 的右子树中
node = node.Right
} else if node.Val > num {
} else if node.Val.(int) > num {
// 目标节点在 cur 的左子树中
node = node.Left
} else {
@@ -127,7 +127,7 @@ comments: true
}
```
=== "JavaScript"
=== "JS"
```javascript title="binary_search_tree.js"
/* 查找节点 */
@@ -147,7 +147,7 @@ comments: true
}
```
=== "TypeScript"
=== "TS"
```typescript title="binary_search_tree.ts"
/* 查找节点 */
@@ -422,7 +422,7 @@ comments: true
return
}
pre = cur
if cur.Val < num {
if cur.Val.(int) < num {
cur = cur.Right
} else {
cur = cur.Left
@@ -430,7 +430,7 @@ comments: true
}
// 插入节点
node := NewTreeNode(num)
if pre.Val < num {
if pre.Val.(int) < num {
pre.Right = node
} else {
pre.Left = node
@@ -438,7 +438,7 @@ comments: true
}
```
=== "JavaScript"
=== "JS"
```javascript title="binary_search_tree.js"
/* 插入节点 */
@@ -464,7 +464,7 @@ comments: true
}
```
=== "TypeScript"
=== "TS"
```typescript title="binary_search_tree.ts"
/* 插入节点 */
@@ -900,7 +900,7 @@ comments: true
break
}
pre = cur
if cur.Val < num {
if cur.Val.(int) < num {
// 待删除节点在右子树中
cur = cur.Right
} else {
@@ -940,14 +940,14 @@ comments: true
tmp = tmp.Left
}
// 递归删除节点 tmp
bst.remove(tmp.Val)
bst.remove(tmp.Val.(int))
// 用 tmp 覆盖 cur
cur.Val = tmp.Val
}
}
```
=== "JavaScript"
=== "JS"
```javascript title="binary_search_tree.js"
/* 删除节点 */
@@ -996,7 +996,7 @@ comments: true
}
```
=== "TypeScript"
=== "TS"
```typescript title="binary_search_tree.ts"
/* 删除节点 */