Skip to content

Commit a9dd3ac

Browse files
committed
Add solution and test-cases for problem 897
1 parent 45f9284 commit a9dd3ac

File tree

5 files changed

+106
-23
lines changed

5 files changed

+106
-23
lines changed

leetcode/801-900/0897.Increasing-Order-Search-Tree/README.md

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,24 @@
11
# [897.Increasing Order Search Tree][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 the `root` of a binary search tree, rearrange the tree in **in-order** so that the leftmost node in the tree is now the root of the tree, and every node has no left child and only one right child.
75

8-
**Example 1:**
6+
**Example 1:**
7+
![example1](./ex1.jpg)
98

109
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
10+
Input: root = [5,3,6,2,4,null,8,1,null,null,null,7,9]
11+
Output: [1,null,2,null,3,null,4,null,5,null,6,null,7,null,8,null,9]
1312
```
1413

15-
## 题意
16-
> ...
14+
**Example 2:**
15+
![example2](./ex2.jpg)
1716

18-
## 题解
1917

20-
### 思路1
21-
> ...
22-
Increasing Order Search Tree
23-
```go
2418
```
25-
19+
Input: root = [5,1,7]
20+
Output: [1,null,5,null,7]
21+
```
2622

2723
## 结语
2824

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

3-
func Solution(x bool) bool {
4-
return x
3+
type TreeNode struct {
4+
Val int
5+
Left, Right *TreeNode
6+
}
7+
8+
func Solution(root *TreeNode) *TreeNode {
9+
if root == nil {
10+
return nil
11+
}
12+
13+
var tree, walker *TreeNode
14+
var inOrder func(*TreeNode)
15+
inOrder = func(root *TreeNode) {
16+
if root == nil {
17+
return
18+
}
19+
if root.Left != nil {
20+
inOrder(root.Left)
21+
}
22+
23+
n := &TreeNode{Val: root.Val}
24+
if tree == nil {
25+
tree = n
26+
walker = n
27+
} else {
28+
walker.Right = n
29+
walker = walker.Right
30+
}
31+
if root.Right != nil {
32+
inOrder(root.Right)
33+
}
34+
}
35+
inOrder(root)
36+
return tree
537
}

leetcode/801-900/0897.Increasing-Order-Search-Tree/Solution_test.go

Lines changed: 62 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,67 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs *TreeNode
14+
expect *TreeNode
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", &TreeNode{
17+
Val: 5,
18+
Left: &TreeNode{
19+
Val: 3,
20+
Right: &TreeNode{Val: 4},
21+
Left: &TreeNode{
22+
Val: 2,
23+
Left: &TreeNode{Val: 1},
24+
},
25+
},
26+
Right: &TreeNode{
27+
Val: 6,
28+
Right: &TreeNode{
29+
Val: 8,
30+
Left: &TreeNode{Val: 7},
31+
Right: &TreeNode{Val: 9},
32+
},
33+
},
34+
}, &TreeNode{
35+
Val: 1,
36+
Right: &TreeNode{
37+
Val: 2,
38+
Right: &TreeNode{
39+
Val: 3,
40+
Right: &TreeNode{
41+
Val: 4,
42+
Right: &TreeNode{
43+
Val: 5,
44+
Right: &TreeNode{
45+
Val: 6,
46+
Right: &TreeNode{
47+
Val: 7,
48+
Right: &TreeNode{
49+
Val: 8,
50+
Right: &TreeNode{
51+
Val: 9,
52+
},
53+
},
54+
},
55+
},
56+
},
57+
},
58+
},
59+
},
60+
}},
61+
{"TestCase2", &TreeNode{
62+
Val: 5,
63+
Left: &TreeNode{Val: 1},
64+
Right: &TreeNode{Val: 7},
65+
}, &TreeNode{
66+
Val: 1,
67+
Right: &TreeNode{
68+
Val: 5,
69+
Right: &TreeNode{
70+
Val: 7,
71+
},
72+
},
73+
}},
1974
}
2075

2176
// 开始测试
@@ -30,10 +85,10 @@ func TestSolution(t *testing.T) {
3085
}
3186
}
3287

33-
// 压力测试
88+
// 压力测试
3489
func BenchmarkSolution(b *testing.B) {
3590
}
3691

37-
// 使用案列
92+
// 使用案列
3893
func ExampleSolution() {
3994
}
42.4 KB
Loading
13 KB
Loading

0 commit comments

Comments
 (0)