给定两个整数数组 preorder 和 inorder ,其中 preorder 是二叉树的先序遍历, inorder 是同一棵树的中序遍历,请构造二叉树并返回其根节点。
func buildTree(preorder []int, inorder []int) *TreeNode {
if len(preorder) == 0 || len(inorder) == 0 {
return nil
}
root := &TreeNode{Val: preorder[0]}
var rootIndex int
for i, v := range inorder {
if v == root.Val {
rootIndex = i
break
}
}
root.Left = buildTree(preorder[1:rootIndex+1], inorder[:rootIndex])
root.Right = buildTree(preorder[rootIndex+1:], inorder[rootIndex+1:])
return root
}