Skip to content

Commit 8266599

Browse files
committed
Merge pull request #203 from 0xff-dev/master
Add solution and test-cases for problem 915
2 parents 3d8548c + 4c38acd commit 8266599

File tree

21 files changed

+378
-93
lines changed

21 files changed

+378
-93
lines changed
Loading

leetcode/1001-1100/1008.Construct-Binary-Search-Tree-from-Preorder-Traversal/README.md

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,29 @@
11
# [1008.Construct Binary Search Tree from Preorder 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)
53

64
## Description
5+
Given an array of integers preorder, which represents the **preorder traversal** of a BST (i.e., **binary search tree**), construct the tree and return its root.
6+
7+
It is **guaranteed** that there is always possible to find a binary search tree with the given requirements for the given test cases.
8+
9+
A **binary search tree** is a binary tree where for every node, any descendant of `Node.left` has a value strictly less than `Node.val`, and any descendant of `Node.right` has a value **strictly greater** than `Node.val`.
10+
11+
A **preorder traversal** of a binary tree displays the value of the node first, then traverses `Node.left`, then traverses `Node.right`.
12+
<br>
713

814
**Example 1:**
915

16+
![tree](./1266.png)
1017
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
18+
Input: preorder = [8,5,1,7,10,12]
19+
Output: [8,5,10,1,7,null,12]
1320
```
1421

15-
## 题意
16-
> ...
22+
**Example 2:**
1723

18-
## 题解
19-
20-
### 思路1
21-
> ...
22-
Construct Binary Search Tree from Preorder Traversal
23-
```go
24+
```
25+
Input: preorder = [1,3]
26+
Output: [1,null,3]
2427
```
2528

2629

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

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(preorder []int) *TreeNode {
4+
return buildBstFromPreOrder(preorder)
5+
}
6+
7+
func buildBstFromPreOrder(pre []int) *TreeNode {
8+
if len(pre) == 0 {
9+
return nil
10+
}
11+
12+
v := pre[0]
13+
node := &TreeNode{Val: v}
14+
idx := 1
15+
for ; idx < len(pre); idx++ {
16+
if pre[idx] > v {
17+
break
18+
}
19+
}
20+
if idx > 1 {
21+
node.Left = buildBstFromPreOrder(pre[1:idx])
22+
}
23+
24+
if idx < len(pre) {
25+
node.Right = buildBstFromPreOrder(pre[idx:])
26+
}
27+
28+
return node
529
}

leetcode/1001-1100/1008.Construct-Binary-Search-Tree-from-Preorder-Traversal/Solution_test.go

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,24 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs []int
14+
expect *TreeNode
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", []int{1}, &TreeNode{Val: 1}},
17+
{"TestCase2", []int{8, 9, 10}, &TreeNode{Val: 8, Right: &TreeNode{Val: 9, Right: &TreeNode{Val: 10}}}},
18+
{"TestCase3", []int{10, 9, 8}, &TreeNode{Val: 10, Left: &TreeNode{Val: 9, Left: &TreeNode{Val: 8}}}},
19+
{"TestCase4", []int{8, 5, 1, 7, 10, 12}, &TreeNode{
20+
Val: 8,
21+
Left: &TreeNode{
22+
Val: 5,
23+
Left: &TreeNode{Val: 1},
24+
Right: &TreeNode{Val: 7},
25+
},
26+
Right: &TreeNode{
27+
Val: 10,
28+
Right: &TreeNode{Val: 12},
29+
},
30+
}},
1931
}
2032

2133
// 开始测试
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package Solution
2+
3+
type TreeNode struct {
4+
Val int
5+
Left *TreeNode
6+
Right *TreeNode
7+
}
Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,19 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(nums []int) *TreeNode {
4+
if len(nums) == 0 {
5+
return nil
6+
}
7+
8+
node := &TreeNode{}
9+
mid := len(nums) / 2
10+
node.Val = nums[mid]
11+
if mid > 0 {
12+
node.Left = Solution(nums[:mid])
13+
}
14+
if mid < len(nums)-1 {
15+
node.Right = Solution(nums[mid+1:])
16+
}
17+
18+
return node
519
}

leetcode/101-200/0108.Convert-Sorted-Array-to-Binary-Search-Tree/Solution_test.go

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,22 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs []int
14+
expect *TreeNode
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", []int{-10, -3, 0, 5, 9}, &TreeNode{
17+
Val: 0,
18+
Left: &TreeNode{
19+
Val: -3,
20+
Left: &TreeNode{Val: -10},
21+
},
22+
Right: &TreeNode{
23+
Val: 9,
24+
Left: &TreeNode{Val: 5},
25+
},
26+
}},
27+
{"TestCase2", []int{1, 3}, &TreeNode{Val: 3, Left: &TreeNode{Val: 1}, Right: nil}},
28+
{"TestCase3", []int{1}, &TreeNode{Val: 1}},
1929
}
2030

2131
// 开始测试
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package Solution
2+
3+
type TreeNode struct {
4+
Val int
5+
Left *TreeNode
6+
Right *TreeNode
7+
}
Lines changed: 70 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,73 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
import "fmt"
4+
5+
func Solution(points [][]int) int {
6+
if len(points) == 1 {
7+
return 1
8+
}
9+
_max := 0
10+
for idx := 1; idx < len(points); idx++ {
11+
tmpSlopes := make(map[string]int)
12+
tmpMax := 0
13+
double := 0
14+
for inner := 0; inner < idx; inner++ {
15+
x1, y1, x2, y2 := points[inner][0], points[inner][1], points[idx][0], points[idx][1]
16+
if x1 == x2 && y1 == y2 {
17+
double++
18+
continue
19+
}
20+
k := slope(x1, y1, x2, y2)
21+
if _, ok := tmpSlopes[k]; !ok {
22+
tmpSlopes[k] = 1
23+
}
24+
tmpSlopes[k]++
25+
if tmpSlopes[k] > _max {
26+
_max = tmpSlopes[k]
27+
}
28+
}
29+
30+
if tmpMax == 0 {
31+
tmpMax = 1
32+
}
33+
tmpMax += double
34+
if tmpMax > _max {
35+
_max = tmpMax
36+
}
37+
}
38+
return _max
39+
}
40+
41+
func slope(x1, y1, x2, y2 int) string {
42+
xx := x2 - x1
43+
yy := y2 - y1
44+
if xx == 0 {
45+
return fmt.Sprintf("%d", x1)
46+
}
47+
if yy == 0 {
48+
return fmt.Sprintf("%d", y1)
49+
}
50+
flag := xx*yy < 0
51+
if xx < 0 {
52+
xx = -xx
53+
}
54+
if yy < 0 {
55+
yy = -yy
56+
}
57+
k := gcd(xx, yy)
58+
yy /= k
59+
xx /= k
60+
s := fmt.Sprintf("%d/%d", yy, xx)
61+
if flag {
62+
return "-" + s
63+
}
64+
return s
65+
}
66+
67+
// 斜率问题,考虑午饭整除的情况
68+
func gcd(x, y int) int {
69+
if y == 0 {
70+
return x
71+
}
72+
return gcd(y, x%y)
573
}

leetcode/101-200/0149.Max-Points-on-a-Line/Solution_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs [][]int
14+
expect int
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", [][]int{{1, 1}, {3, 2}, {5, 3}, {4, 1}, {2, 3}, {1, 4}}, 4},
17+
{"TestCase2", [][]int{{1, 1}, {2, 2}, {3, 3}}, 3},
18+
{"TestCase3", [][]int{{0, 0}, {4, 5}, {7, 8}, {8, 9}, {5, 6}, {3, 4}, {1, 1}}, 5},
1919
}
2020

2121
// 开始测试

leetcode/1101-1200/1104.Path-In-Zigzag-Labelled-Binary-Tree/README.md

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,27 @@
11
# [1104.Path In Zigzag Labelled Binary 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+
In an infinite binary tree where every node has two children, the nodes are labelled in row order.
5+
6+
In the odd numbered rows (ie., the first, third, fifth,...), the labelling is left to right, while in the even numbered rows (second, fourth, sixth,...), the labelling is right to left.
7+
8+
![image](./tree.png)
9+
10+
Given the `label` of a node in this tree, return the labels in the path from the root of the tree to the node with that `label`.
711

812
**Example 1:**
913

1014
```
11-
Input: a = "11", b = "1"
12-
Output: "100"
15+
Input: label = 14
16+
Output: [1,3,4,14]
1317
```
1418

15-
## 题意
16-
> ...
19+
__Example 2:__
1720

18-
## 题解
19-
20-
### 思路1
21-
> ...
22-
Path In Zigzag Labelled Binary Tree
23-
```go
2421
```
25-
22+
Input: label = 26
23+
Output: [1,2,6,10,26]
24+
```
2625

2726
## 结语
2827

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

3-
func Solution(x bool) bool {
4-
return x
3+
func Solution(label int) []int {
4+
res := make([]int, 0)
5+
idx, base := 0, 1
6+
for ; base <= label; base, idx = base*2, idx+1 {
7+
}
8+
9+
base, idx = base/2, idx-1
10+
for {
11+
nextBase, nextIdx := base/2, idx-1
12+
res = append(res, label)
13+
if nextIdx < 0 {
14+
break
15+
}
16+
diff := (label - base) / 2
17+
label = base - 1 - diff
18+
base, idx = nextBase, nextIdx
19+
}
20+
21+
for s, e := 0, len(res)-1; s < e; s, e = s+1, e-1 {
22+
res[s], res[e] = res[e], res[s]
23+
}
24+
25+
return res
526
}

leetcode/1101-1200/1104.Path-In-Zigzag-Labelled-Binary-Tree/Solution_test.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,13 @@ func TestSolution(t *testing.T) {
1010
// 测试用例
1111
cases := []struct {
1212
name string
13-
inputs bool
14-
expect bool
13+
inputs int
14+
expect []int
1515
}{
16-
{"TestCase", true, true},
17-
{"TestCase", true, true},
18-
{"TestCase", false, false},
16+
{"TestCase1", 1, []int{1}},
17+
{"TestCase2", 14, []int{1, 3, 4, 14}},
18+
{"TestCase3", 26, []int{1, 2, 6, 10, 26}},
19+
{"TestCase4", 1234, []int{1, 3, 4, 14, 19, 57, 77, 229, 308, 918, 1234}},
1920
}
2021

2122
// 开始测试
Loading

0 commit comments

Comments
 (0)