diff --git a/Algorithms/0105. Construct Binary Tree from Preorder and Inorder Traversal/105. Construct Binary Tree from Preorder and Inorder Traversal.go b/Algorithms/0105. Construct Binary Tree from Preorder and Inorder Traversal/105. Construct Binary Tree from Preorder and Inorder Traversal.go new file mode 100644 index 00000000..983c78d3 --- /dev/null +++ b/Algorithms/0105. Construct Binary Tree from Preorder and Inorder Traversal/105. Construct Binary Tree from Preorder and Inorder Traversal.go @@ -0,0 +1,29 @@ +package leetcode + +/** + * 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 +} diff --git a/Algorithms/0105. Construct Binary Tree from Preorder and Inorder Traversal/105. Construct Binary Tree from Preorder and Inorder Traversal_test.go b/Algorithms/0105. Construct Binary Tree from Preorder and Inorder Traversal/105. Construct Binary Tree from Preorder and Inorder Traversal_test.go new file mode 100644 index 00000000..ac9e32b4 --- /dev/null +++ b/Algorithms/0105. Construct Binary Tree from Preorder and Inorder Traversal/105. Construct Binary Tree from Preorder and Inorder Traversal_test.go @@ -0,0 +1,44 @@ +package leetcode + +import ( + "fmt" + "testing" +) + +type question105 struct { + para105 + ans105 +} + +// para 是参数 +// one 代表第一个参数 +type para105 struct { + preorder []int + inorder []int +} + +// ans 是答案 +// one 代表第一个答案 +type ans105 struct { + one []int +} + +func Test_Problem105(t *testing.T) { + + qs := []question105{ + + question105{ + para105{[]int{3, 9, 20, 15, 7}, []int{9, 3, 15, 20, 7}}, + ans105{[]int{3, 9, 20, NULL, NULL, 15, 7}}, + }, + } + + fmt.Printf("------------------------Leetcode Problem 105------------------------\n") + + for _, q := range qs { + _, p := q.ans105, q.para105 + fmt.Printf("【input】:%v ", p) + fmt.Printf("【output】:%v \n", Tree2ints(buildTree(p.preorder, p.inorder))) + } + fmt.Printf("\n\n\n") +} diff --git a/Algorithms/0105. Construct Binary Tree from Preorder and Inorder Traversal/README.md b/Algorithms/0105. Construct Binary Tree from Preorder and Inorder Traversal/README.md new file mode 100755 index 00000000..a1d6526a --- /dev/null +++ b/Algorithms/0105. Construct Binary Tree from Preorder and Inorder Traversal/README.md @@ -0,0 +1,36 @@ +# [105. Construct Binary Tree from Preorder and Inorder Traversal](https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/) + + +## 题目: + +Given preorder and inorder traversal of a tree, construct the binary tree. + +**Note:**You may assume that duplicates do not exist in the tree. + +For example, given + + preorder = [3,9,20,15,7] + inorder = [9,3,15,20,7] + +Return the following binary tree: + + 3 + / \ + 9 20 + / \ + 15 7 + + + +## 题目大意 + +根据一棵树的前序遍历与中序遍历构造二叉树。 + +注意: +你可以假设树中没有重复的元素。 + + +## 解题思路 + +- 给出 2 个数组,根据 preorder 和 inorder 数组构造一颗树。 +- 利用递归思想,从 preorder 可以得到根节点,从 inorder 中得到左子树和右子树。只剩一个节点的时候即为根节点。不断的递归直到所有的树都生成完成。