hello-algo/codes/go/chapter_divide_and_conquer/build_tree.go
Hongyun Zhang eb695937a4
Add divide and conquer go code (#638)
* feat: Add Go code to binary search recursion under divide and conquer

* style: Code comment standardization

* style: modify function comment

* fix: fixed error when the list is empty

* Update print_utils.go

* Delete print_utils_test.go

* feat: add Go code to divide and conquer

* Update build_tree.go

* style: modify function name

---------

Co-authored-by: Yudong Jin <krahets@163.com>
2023-07-21 22:16:42 +08:00

37 lines
1 KiB
Go

// File: build_tree.go
// Created Time: 2023-07-20
// Author: hongyun-robot (1836017030@qq.com)
package chapter_divide_and_conquer
import . "github.com/krahets/hello-algo/pkg"
/* 构建二叉树:分治 */
func dfsBuildTree(preorder, inorder []int, hmap map[int]int, i, l, r int) *TreeNode {
// 子树区间为空时终止
if r-l < 0 {
return nil
}
// 初始化根节点
root := NewTreeNode(preorder[i])
// 查询 m ,从而划分左右子树
m := hmap[preorder[i]]
// 子问题:构建左子树
root.Left = dfsBuildTree(preorder, inorder, hmap, i+1, l, m-1)
// 子问题:构建右子树
root.Right = dfsBuildTree(preorder, inorder, hmap, i+1+m-l, m+1, r)
// 返回根节点
return root
}
/* 构建二叉树 */
func buildTree(preorder, inorder []int) *TreeNode {
// 初始化哈希表,存储 inorder 元素到索引的映射
hmap := make(map[int]int, len(inorder))
for i := 0; i < len(inorder); i++ {
hmap[inorder[i]] = i
}
root := dfsBuildTree(preorder, inorder, hmap, 0, 0, len(inorder)-1)
return root
}