This commit is contained in:
krahets
2024-03-31 03:53:04 +08:00
parent 87af663929
commit c23e576da4
68 changed files with 2139 additions and 22 deletions

View File

@ -183,6 +183,16 @@ comments: true
=== "Kotlin"
```kotlin title=""
/* 二叉树节点类 */
class TreeNode(val _val: Int) { // 节点值
val left: TreeNode? = null // 左子节点引用
val right: TreeNode? = null // 右子节点引用
}
```
=== "Ruby"
```ruby title=""
```
@ -414,6 +424,22 @@ comments: true
=== "Kotlin"
```kotlin title="binary_tree.kt"
// 初始化节点
val n1 = TreeNode(1)
val n2 = TreeNode(2)
val n3 = TreeNode(3)
val n4 = TreeNode(4)
val n5 = TreeNode(5)
// 构建节点之间的引用(指针)
n1.left = n2
n1.right = n3
n2.left = n4
n2.right = n5
```
=== "Ruby"
```ruby title="binary_tree.rb"
```
@ -568,6 +594,17 @@ comments: true
=== "Kotlin"
```kotlin title="binary_tree.kt"
val P = TreeNode(0)
// 在 n1 -> n2 中间插入节点 P
n1.left = P
P.left = n2
// 删除节点 P
n1.left = n2
```
=== "Ruby"
```ruby title="binary_tree.rb"
```