mirror of
https://github.com/krahets/hello-algo.git
synced 2026-04-02 10:13:00 +08:00
Add kotlin code for chapter_stack_and_queue and chapter_tree (#1197)
* Add kotlin code block for chapter_hashing * Add kotlin code block for chapter_heap. * Add kotlin code block for chapter_stack_and_queue and chapter_tree * fix indentation * Update binary_tree.md --------- Co-authored-by: Yudong Jin <krahets@163.com>
This commit is contained in:
@@ -179,7 +179,11 @@
|
||||
=== "Kotlin"
|
||||
|
||||
```kotlin title=""
|
||||
|
||||
/* 二叉树节点类 */
|
||||
class TreeNode(val _val: Int) { // 节点值
|
||||
val left: TreeNode? = null // 左子节点引用
|
||||
val right: TreeNode? = null // 右子节点引用
|
||||
}
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
@@ -406,7 +410,17 @@
|
||||
=== "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
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
@@ -557,7 +571,12 @@
|
||||
=== "Kotlin"
|
||||
|
||||
```kotlin title="binary_tree.kt"
|
||||
|
||||
val P = TreeNode(0)
|
||||
// 在 n1 -> n2 中间插入节点 P
|
||||
n1.left = P
|
||||
P.left = n2
|
||||
// 删除节点 P
|
||||
n1.left = n2
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
|
||||
Reference in New Issue
Block a user