mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-25 03:11:41 +08:00
38 lines
919 B
Go
38 lines
919 B
Go
package leetcode
|
|
|
|
import (
|
|
"github.com/halfrost/LeetCode-Go/structures"
|
|
)
|
|
|
|
// TreeNode define
|
|
type TreeNode = structures.TreeNode
|
|
|
|
/**
|
|
* Definition for a binary tree node.
|
|
* type TreeNode struct {
|
|
* Val int
|
|
* Left *TreeNode
|
|
* Right *TreeNode
|
|
* }
|
|
*/
|
|
|
|
func buildTree(preorder []int, inorder []int) *TreeNode {
|
|
inPos := make(map[int]int)
|
|
for i := 0; i < len(inorder); i++ {
|
|
inPos[inorder[i]] = i
|
|
}
|
|
return buildPreIn2TreeDFS(preorder, 0, len(preorder)-1, 0, inPos)
|
|
}
|
|
|
|
func buildPreIn2TreeDFS(pre []int, preStart int, preEnd int, inStart int, inPos map[int]int) *TreeNode {
|
|
if preStart > preEnd {
|
|
return nil
|
|
}
|
|
root := &TreeNode{Val: pre[preStart]}
|
|
rootIdx := inPos[pre[preStart]]
|
|
leftLen := rootIdx - inStart
|
|
root.Left = buildPreIn2TreeDFS(pre, preStart+1, preStart+leftLen, inStart, inPos)
|
|
root.Right = buildPreIn2TreeDFS(pre, preStart+leftLen+1, preEnd, rootIdx+1, inPos)
|
|
return root
|
|
}
|