Skip to content

Commit 505eea8

Browse files
authored
Merge pull request #1131 from 0xff-dev/889
Add solution and test-cases for problem 889
2 parents 5a80c74 + 60d09e7 commit 505eea8

File tree

4 files changed

+74
-28
lines changed

4 files changed

+74
-28
lines changed
Loading

leetcode/801-900/0889.Construct-Binary-Tree-from-Preorder-and-Postorder-Traversal/README.md

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,25 @@
11
# [889.Construct Binary Tree from Preorder and Postorder Traversal][title]
22

3-
> [!WARNING|style:flat]
4-
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5-
63
## Description
4+
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.
5+
6+
If there exist multiple answers, you can **return any** of them.
7+
8+
**Example 1:**
79

8-
**Example 1:**
10+
![1](./1.jpg)
911

1012
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
13+
Input: preorder = [1,2,4,5,3,6,7], postorder = [4,5,2,6,7,3,1]
14+
Output: [1,2,3,4,5,6,7]
1315
```
1416

15-
## 题意
16-
> ...
17+
**Example 2:**
1718

18-
## 题解
19-
20-
### 思路1
21-
> ...
22-
Construct Binary Tree from Preorder and Postorder Traversal
23-
```go
2419
```
25-
20+
Input: preorder = [1], postorder = [1]
21+
Output: [1]
22+
```
2623

2724
## 结语
2825

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,39 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
type TreeNode struct {
4+
Val int
5+
Left *TreeNode
6+
Right *TreeNode
7+
}
8+
9+
func Solution(preorder []int, postorder []int) *TreeNode {
10+
if len(preorder) == 0 {
11+
return nil
12+
}
13+
root := &TreeNode{
14+
Val: preorder[0],
15+
}
16+
if len(preorder) == 1 {
17+
return root
18+
}
19+
20+
postIndex := 0
21+
next := preorder[1]
22+
for i := range postorder {
23+
if postorder[i] == next {
24+
postIndex = i
25+
break
26+
}
27+
}
28+
preIndex := 0
29+
m := map[int]int{}
30+
for i, n := range preorder {
31+
m[n] = i
32+
}
33+
for i := 0; i <= postIndex; i++ {
34+
preIndex = max(preIndex, m[postorder[i]])
35+
}
36+
root.Left = Solution(preorder[1:preIndex+1], postorder[:postIndex+1])
37+
root.Right = Solution(preorder[preIndex+1:], postorder[postIndex+1:len(postorder)-1])
38+
return root
539
}

leetcode/801-900/0889.Construct-Binary-Tree-from-Preorder-and-Postorder-Traversal/Solution_test.go

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,31 +9,46 @@ import (
99
func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
12-
name string
13-
inputs bool
14-
expect bool
12+
name string
13+
preorder, postorder []int
14+
expect *TreeNode
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", []int{1, 2, 4, 5, 3, 6, 7}, []int{4, 5, 2, 6, 7, 3, 1}, &TreeNode{
17+
Val: 1,
18+
Left: &TreeNode{
19+
Val: 2,
20+
Left: &TreeNode{
21+
Val: 4,
22+
},
23+
Right: &TreeNode{Val: 5},
24+
},
25+
Right: &TreeNode{
26+
Val: 3,
27+
Left: &TreeNode{
28+
Val: 6,
29+
},
30+
Right: &TreeNode{Val: 7},
31+
},
32+
}},
33+
{"TestCase2", []int{1}, []int{1}, &TreeNode{Val: 1}},
1934
}
2035

2136
// 开始测试
2237
for i, c := range cases {
2338
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
24-
got := Solution(c.inputs)
39+
got := Solution(c.preorder, c.postorder)
2540
if !reflect.DeepEqual(got, c.expect) {
26-
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
27-
c.expect, got, c.inputs)
41+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
42+
c.expect, got, c.preorder, c.postorder)
2843
}
2944
})
3045
}
3146
}
3247

33-
// 压力测试
48+
// 压力测试
3449
func BenchmarkSolution(b *testing.B) {
3550
}
3651

37-
// 使用案列
52+
// 使用案列
3853
func ExampleSolution() {
3954
}

0 commit comments

Comments
 (0)