diff --git a/leetcode/801-900/0889.Construct-Binary-Tree-from-Preorder-and-Postorder-Traversal/1.jpg b/leetcode/801-900/0889.Construct-Binary-Tree-from-Preorder-and-Postorder-Traversal/1.jpg new file mode 100644 index 000000000..3ae97b5f8 Binary files /dev/null and b/leetcode/801-900/0889.Construct-Binary-Tree-from-Preorder-and-Postorder-Traversal/1.jpg differ diff --git a/leetcode/801-900/0889.Construct-Binary-Tree-from-Preorder-and-Postorder-Traversal/README.md b/leetcode/801-900/0889.Construct-Binary-Tree-from-Preorder-and-Postorder-Traversal/README.md index 5d6551c82..59c80e988 100644 --- a/leetcode/801-900/0889.Construct-Binary-Tree-from-Preorder-and-Postorder-Traversal/README.md +++ b/leetcode/801-900/0889.Construct-Binary-Tree-from-Preorder-and-Postorder-Traversal/README.md @@ -1,28 +1,25 @@ # [889.Construct Binary Tree from Preorder and Postorder Traversal][title] -> [!WARNING|style:flat] -> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm) - ## Description +Given two integer arrays, `preorder` and `postorder` where preorder is the preorder traversal of a binary tree of **distinct** values and `postorder` is the postorder traversal of the same tree, reconstruct and return the binary tree. + +If there exist multiple answers, you can **return any** of them. + +**Example 1:** -**Example 1:** +![1](./1.jpg) ``` -Input: a = "11", b = "1" -Output: "100" +Input: preorder = [1,2,4,5,3,6,7], postorder = [4,5,2,6,7,3,1] +Output: [1,2,3,4,5,6,7] ``` -## 题意 -> ... +**Example 2:** -## 题解 - -### 思路1 -> ... -Construct Binary Tree from Preorder and Postorder Traversal -```go ``` - +Input: preorder = [1], postorder = [1] +Output: [1] +``` ## 结语 diff --git a/leetcode/801-900/0889.Construct-Binary-Tree-from-Preorder-and-Postorder-Traversal/Solution.go b/leetcode/801-900/0889.Construct-Binary-Tree-from-Preorder-and-Postorder-Traversal/Solution.go index d115ccf5e..6eb9d9c2e 100644 --- a/leetcode/801-900/0889.Construct-Binary-Tree-from-Preorder-and-Postorder-Traversal/Solution.go +++ b/leetcode/801-900/0889.Construct-Binary-Tree-from-Preorder-and-Postorder-Traversal/Solution.go @@ -1,5 +1,39 @@ package Solution -func Solution(x bool) bool { - return x +type TreeNode struct { + Val int + Left *TreeNode + Right *TreeNode +} + +func Solution(preorder []int, postorder []int) *TreeNode { + if len(preorder) == 0 { + return nil + } + root := &TreeNode{ + Val: preorder[0], + } + if len(preorder) == 1 { + return root + } + + postIndex := 0 + next := preorder[1] + for i := range postorder { + if postorder[i] == next { + postIndex = i + break + } + } + preIndex := 0 + m := map[int]int{} + for i, n := range preorder { + m[n] = i + } + for i := 0; i <= postIndex; i++ { + preIndex = max(preIndex, m[postorder[i]]) + } + root.Left = Solution(preorder[1:preIndex+1], postorder[:postIndex+1]) + root.Right = Solution(preorder[preIndex+1:], postorder[postIndex+1:len(postorder)-1]) + return root } diff --git a/leetcode/801-900/0889.Construct-Binary-Tree-from-Preorder-and-Postorder-Traversal/Solution_test.go b/leetcode/801-900/0889.Construct-Binary-Tree-from-Preorder-and-Postorder-Traversal/Solution_test.go index 14ff50eb4..a9526eb30 100644 --- a/leetcode/801-900/0889.Construct-Binary-Tree-from-Preorder-and-Postorder-Traversal/Solution_test.go +++ b/leetcode/801-900/0889.Construct-Binary-Tree-from-Preorder-and-Postorder-Traversal/Solution_test.go @@ -9,31 +9,46 @@ import ( func TestSolution(t *testing.T) { // 测试用例 cases := []struct { - name string - inputs bool - expect bool + name string + preorder, postorder []int + expect *TreeNode }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", []int{1, 2, 4, 5, 3, 6, 7}, []int{4, 5, 2, 6, 7, 3, 1}, &TreeNode{ + Val: 1, + Left: &TreeNode{ + Val: 2, + Left: &TreeNode{ + Val: 4, + }, + Right: &TreeNode{Val: 5}, + }, + Right: &TreeNode{ + Val: 3, + Left: &TreeNode{ + Val: 6, + }, + Right: &TreeNode{Val: 7}, + }, + }}, + {"TestCase2", []int{1}, []int{1}, &TreeNode{Val: 1}}, } // 开始测试 for i, c := range cases { t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) { - got := Solution(c.inputs) + got := Solution(c.preorder, c.postorder) if !reflect.DeepEqual(got, c.expect) { - t.Fatalf("expected: %v, but got: %v, with inputs: %v", - c.expect, got, c.inputs) + t.Fatalf("expected: %v, but got: %v, with inputs: %v %v", + c.expect, got, c.preorder, c.postorder) } }) } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }