8000 Add binary-tree-zigzag-level-order-traversal and tidy code · beyondyyh/Leetcode@1a36737 · GitHub
[go: up one dir, main page]

Skip to content

Commit 1a36737

Browse files
committed
Add binary-tree-zigzag-level-order-traversal and tidy code
1 parent de3bb0f commit 1a36737

14 files changed

+117
-36
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
*.sw*
88

99
# Test binary, build with `go test -c`
10+
coverage.txt
1011
*.test
1112

1213
# Output of the go coverage tool, specifically when used with LiteIDE

README.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010
Tests
1111

1212
```shell
13-
go test -run regexp
14-
Run only those tests and examples matching the regular expression.
15-
16-
go test -run $GOPATH/src/gopl.io/interview2020/Leetcode/algorithms/my*
13+
cd yourpath/Leetcode
14+
sh test.sh
1715
```

algorithms/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
|0094|[二叉树的中序遍历](https://leetcode-cn.com/problems/binary-tree-inorder-traversal/)|[go](./tree/94.inorderTraversal.go)|M|
2828
|0101|[对称二叉树](https://leetcode-cn.com/problems/symmetric-tree/submissions/)|[go](./tree/101.isSymmetric.go)|S|
2929
|0102|[二叉树的层次遍历](https://leetcode-cn.com/problems/binary-tree-level-order-traversal/submissions/)|[go](./tree/102.levelOrder.go)|M|
30+
|0103|[二叉树的锯齿形层次遍历](https://leetcode-cn.com/problems/binary-tree-level-order-traversal/submissions/)|[go](./tree/103.zigzagLevelOrder.go)|M|
3031
|0104|[二叉树的最大深度](https://leetcode-cn.com/problems/maximum-depth-of-binary-tree/)|[go](./tree/104.maxDepth.go)|S|
3132
|0118|[杨辉三角](https://leetcode-cn.com/problems/pascals-triangle/)|[go](./myarray/118.generate.go)|S|
3233
|0121|[买卖股票的最佳时机](https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock/)|[go](./myarray/121.maxProfit.go)|S|

algorithms/other/array.Quicksort.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
package main
1+
package other
22

33
import "fmt"
44

5-
// 快排,会延伸很多变种
5+
// QuickSort 快排,会延伸很多变种
66
func QuickSort(data []int) []int {
77
if len(data) <= 1 {
88
return data
@@ -28,8 +28,8 @@ func QuickSort(data []int) []int {
2828
return data
2929
}
3030

31-
func main() {
32-
nums := []int{8, 9, 1, 3, 2, 0, 7, 11}
33-
fmt.Printf("befort: %v\n", nums)
34-
fmt.Printf("after: %v\n", QuickSort(nums))
35-
}
31+
// func main() {
32+
// nums := []int{8, 9, 1, 3, 2, 0, 7, 11}
33+
// fmt.Printf("befort: %v\n", nums)
34+
// fmt.Printf("after: %v\n", QuickSort(nums))
35+
// }

algorithms/other/array.binarySearch.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
package main
2-
3-
import "fmt"
1+
package other
42

53
// 二分查找
64
func binarySearch(nums []int, target int) bool {
@@ -19,7 +17,7 @@ func binarySearch(nums []int, target int) bool {
1917
return false
2018
}
2119

22-
func main() {
23-
nums := []int{-1, 0, 1, 2, 3, 4, 5, 10, 11, 19}
24-
fmt.Println(binarySearch(nums, 1))
25-
}
20+
// func main() {
21+
// nums := []int{-1, 0, 1, 2, 3, 4, 5, 10, 11, 19}
22+
// fmt.Println(binarySearch(nums, 1))
23+
// }

algorithms/other/array.findMid.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package main
1+
package other
22

33
// 题目:给定一个无序的数字,找出该数组的中位数
44
// 解题思路:借助快排思想,如果

algorithms/other/string.ip2long.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
package main
1+
package other
22

33
import (
44
"encoding/binary"
5-
"fmt"
65
"net"
76
)
87

8+
// IP2long returns unit32
99
func IP2long(ipstr string) uint32 {
1010
ip := net.ParseIP(ipstr)
1111
if ip == nil {
@@ -15,7 +15,7 @@ func IP2long(ipstr string) uint32 {
1515
return binary.BigEndian.Uint32(ip)
1616
}
1717

18-
func main() {
19-
s := "127.0.0.1"
20-
fmt.Println(IP2long(s))
21-
}
18+
// func main() {
19+
// s := "127.0.0.1"
20+
// fmt.Println(IP2long(s))
21+
// }

algorithms/other/string.sortStringOnly.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
1-
package main
1+
package other
22

33
import (
4-
"fmt"
54
"sort"
65
)
76

87
// 题目:给定一个字符串,带数字字母和其他特殊字符,要求只给字母排序,其他字符串位置保持不变
98
// 例如:input: cb12$%a2A output: Aa12$%b2c
10-
func main() {
11-
str := "09cbAa12d98xYz~#"
12-
// str = "cb12$%a2A"
13-
fmt.Printf("origin str: %s\n", str)
14-
fmt.Println(sortStringOnly(str))
15-
}
9+
// func main() {
10+
// str := "09cbAa12d98xYz~#"
11+
// // str = "cb12$%a2A"
12+
// fmt.Printf("origin str: %s\n", str)
13+
// fmt.Println(sortStringOnly(str))
14+
// }
1615

1716
func sortStringOnly(str string) string {
1817
var (

algorithms/other/tree.BreadthFirstSearch.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
package main
1+
package other
22

3+
// TreeNode declare
34
type TreeNode struct {
45
Data interface{}
56
Left *TreeNode
67
Right *TreeNode
78
}
89

9-
// 广度优先遍历
10+
// BreadthFirstSearch 广度优先遍历
1011
func BreadthFirstSearch(root TreeNode) []interface{} {
1112
var res []interface{}
1213
var nodes []TreeNode = []TreeNode{root}

algorithms/tree/102.levelOrder.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@ func BFS(root *TreeNode) []int { // {{{
2828
// 2. 如果根据等级来保存[][]int的话 只要声明一个正在遍历的队列 一个保存下一层节点的队列即可
2929
func levelOrder1(root *TreeNode) [][]int { // {{{
3030
var res [][]int
31-
var queue []*TreeNode = []*TreeNode{root}
3231
if root == nil {
3332
return res
3433
}
3534

35+
var queue []*TreeNode = []*TreeNode{root}
3636
for len(queue) > 0 {
3737
var tmpQueue []*TreeNode
3838
var tmpRes []int
@@ -52,6 +52,7 @@ func levelOrder1(root *TreeNode) [][]int { // {{{
5252
queue = append(queue, tmpQueue...)
5353
res = append(res, tmpRes)
5454
}
55+
5556
return res
5657
} // }}}
5758

0 commit comments

Comments
 (0)
0