mirror of
https://github.com/krahets/hello-algo.git
synced 2026-07-02 02:24:24 +00:00
build
This commit is contained in:
@@ -449,7 +449,13 @@ comments: true
|
||||
|
||||
```kotlin title="build_tree.kt"
|
||||
/* 构建二叉树:分治 */
|
||||
fun dfs(preorder: IntArray, inorderMap: Map<Int?, Int?>, i: Int, l: Int, r: Int): TreeNode? {
|
||||
fun dfs(
|
||||
preorder: IntArray,
|
||||
inorderMap: Map<Int?, Int?>,
|
||||
i: Int,
|
||||
l: Int,
|
||||
r: Int
|
||||
): TreeNode? {
|
||||
// 子树区间为空时终止
|
||||
if (r - l < 0) return null
|
||||
// 初始化根节点
|
||||
@@ -467,7 +473,7 @@ comments: true
|
||||
/* 构建二叉树 */
|
||||
fun buildTree(preorder: IntArray, inorder: IntArray): TreeNode? {
|
||||
// 初始化哈希表,存储 inorder 元素到索引的映射
|
||||
val inorderMap: MutableMap<Int?, Int?> = HashMap()
|
||||
val inorderMap = HashMap<Int?, Int?>()
|
||||
for (i in inorder.indices) {
|
||||
inorderMap[inorder[i]] = i
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user