Skip to content

Add solution and test-cases for problem 889 #1131

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -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]
```

## 结语

Expand Down
Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
}
Loading