Skip to content

Commit 9c27742

Browse files
committed
Merge branch 'develop'
# Conflicts: # SUMMARY.md # docs/jzof/of012.md
2 parents c6b3903 + 8542f69 commit 9c27742

File tree

6 files changed

+148
-26
lines changed

6 files changed

+148
-26
lines changed

docs/jzof/of007.md

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
---
2+
description: 剑指 Offer 07. 重建二叉树
3+
---
4+
5+
# OF6.重建二叉树
6+
7+
## 题目描述
8+
9+
[题目地址](https://leetcode-cn.com/problems/zhong-jian-er-cha-shu-lcof/)
10+
11+
输入某二叉树的前序遍历和中序遍历的结果,请重建该二叉树。假设输入的前序遍历和中序遍历的结果中都不含重复的数字。
12+
13+
14+
### **示例 1:**
15+
16+
```go
17+
前序遍历 preorder = [3,9,20,15,7]
18+
中序遍历 inorder = [9,3,15,20,7]
19+
20+
3
21+
/ \
22+
9 20
23+
/ \
24+
15 7
25+
```
26+
27+
## 题解
28+
29+
### 思路1 : 递归
30+
31+
递归遍历
32+
33+
**算法流程:**
34+
35+
1. **复杂度分析:**
36+
2. **时间复杂度**$$O(N)$$****遍历N次,递归 N 次
37+
3. **空间复杂度**$$O(N)$$****递归 N 次,开辟 N 个栈空间
38+
39+
#### 代码
40+
41+
{% tabs %}
42+
{% tab title="Go" %}
43+
```go
44+
func reversePrint(head *ListNode) []int {
45+
ans := make([]int, 0)
46+
if head == nil {
47+
return ans
48+
}
49+
ans = reversePrint(head.Next)
50+
ans = append(ans, head.Val)
51+
return ans
52+
}
53+
```
54+
{% endtab %}
55+
{% endtabs %}
56+
57+
### 思路1 : 多指针
58+
59+
多个指针辅助,一次遍历
60+
61+
**算法流程:**
62+
63+
1. **复杂度分析:**
64+
2. **时间复杂度**$$O(N)$$****遍历N次,递归 N 次
65+
3. **空间复杂度**$$O(N)$$****递归 N 次,开辟 N 个栈空间
66+
67+
#### 代码
68+
69+
{% tabs %}
70+
{% tab title="Go" %}
71+
```go
72+
func reversePrint(head *ListNode) []int {
73+
if head == nil {
74+
return []int{}
75+
}
76+
pre, cur, next, ans := &ListNode{}, head, head.Next, []int{}
77+
for cur != nil {
78+
next = cur.Next
79+
cur.Next = pre
80+
81+
pre = cur
82+
cur = next
83+
}
84+
for pre.Next != nil {
85+
ans = append(ans, pre.Val)
86+
pre = pre.Next
87+
}
88+
return ans
89+
}
90+
```
91+
{% endtab %}
92+
{% endtabs %}
93+
94+
## 总结
95+
96+
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 算法 题解:[awesome-golang-algorithm](https://github.com/kylesliu/awesome-golang-algorithm)
97+

lcof/of000/Solution_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func TestSolution(t *testing.T) {
4343
for _, c := range cases {
4444
t.Run(c.name, func(t *testing.T) {
4545
actual := f(c.inputs)
46-
ast.Equal(c.expect, actual,
46+
ast.Equal(c, actual,
4747
"func: %v case: %v ",
4848
runtime.FuncForPC(reflect.ValueOf(f).Pointer()).Name(), c.name)
4949
})

lcof/of012/Solution.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@ func exist(board [][]byte, word string) bool {
55
for i := 0; i < m; i++ {
66
for j := 0; j < n; j++ {
77
//如果在数组中找得到第一个数,就执行下一步,否则返回false
8-
if search(board, i, j, 0, word) {
8+
if dfs(board, i, j, word) {
99
return true
1010
}
1111
}
1212
}
1313
return false
1414
}
15-
func search(board [][]byte, i, j, k int, word string) bool {
15+
func dfs(board [][]byte, i, j int, word string) bool {
1616
//如果找到最后一个数,则返回true,搜索成功
17-
if k == len(word) {
17+
if len(word) == 0 {
1818
return true
1919
}
2020
//i,j的约束条件
@@ -25,15 +25,15 @@ func search(board [][]byte, i, j, k int, word string) bool {
2525
//进入DFS深度优先搜索
2626
//先把正在遍历的该值重新赋值,如果在该值的周围都搜索不到目标字符,则再把该值还原
2727
//如果在数组中找到第一个字符,则进入下一个字符的查找
28-
if board[i][j] == word[k] {
28+
if board[i][j] == word[0] {
2929
temp := board[i][j]
3030
board[i][j] = ' '
3131
//下面这个if语句,如果成功进入,说明找到该字符,然后进行下一个字符的搜索,直到所有的搜索都成功,
3232
//即k == len(word) - 1 的大小时,会返回true,进入该条件语句,然后返回函数true值。
33-
if search(board, i, j+1, k+1, word) ||
34-
search(board, i, j-1, k+1, word) ||
35-
search(board, i+1, j, k+1, word) ||
36-
search(board, i-1, j, k+1, word) {
33+
if dfs(board, i, j+1, word[1:]) ||
34+
dfs(board, i, j-1, word[1:]) ||
35+
dfs(board, i+1, j, word[1:]) ||
36+
dfs(board, i-1, j, word[1:]) {
3737
return true
3838
} else {
3939
//没有找到目标字符,还原之前重新赋值的board[i][j]

lcof/of012/solution.py

Whitespace-only changes.

leetcode/1101-1200/1161.Maximum-Level-Sum-of-a-Binary-Tree/Solution.go

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
package Solution
22

3-
/**
4-
* Definition for a binary tree node.
5-
* type TreeNode struct {
6-
* Val int
7-
* Left *TreeNode
8-
* Right *TreeNode
9-
* }
10-
*/
3+
import (
4+
"fmt"
5+
)
6+
7+
type TreeNode struct {
8+
Val int
9+
Left *TreeNode
10+
Right *TreeNode
11+
}
1112

1213
func maxLevelSum(root *TreeNode) int {
1314
tree := make(map[*TreeNode]int)
@@ -51,3 +52,27 @@ func maxLevelSum(root *TreeNode) int {
5152
}
5253
return l + 1
5354
}
55+
56+
// DFS 递归的中序遍历
57+
func maxLevelSum2(root *TreeNode) int {
58+
m, ans := make(map[int]int), -1
59+
dfs(root, 1, m)
60+
fmt.Println(m)
61+
for _, v := range m {
62+
if v > ans {
63+
ans = v
64+
}
65+
print(v)
66+
}
67+
return ans
68+
}
69+
70+
func dfs(root *TreeNode, level int, m map[int]int) {
71+
if root == nil {
72+
return
73+
}
74+
fmt.Println(level, root.Val)
75+
dfs(root.Left, level+1, m)
76+
m[level] += root.Val
77+
dfs(root.Right, level+1, m)
78+
}

leetcode/1101-1200/1161.Maximum-Level-Sum-of-a-Binary-Tree/Solution_test.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,11 @@ import (
88
"github.com/stretchr/testify/assert"
99
)
1010

11-
type TreeNode struct {
12-
Val int
13-
Left *TreeNode
14-
Right *TreeNode
15-
}
16-
1711
type SolutionFuncType func(*TreeNode) int
1812

1913
var SolutionFuncList = []SolutionFuncType{
20-
maxLevelSum,
14+
//maxLevelSum,
15+
maxLevelSum2,
2116
}
2217

2318
var DefaultValue int = -1024
@@ -58,15 +53,20 @@ func TestSolution(t *testing.T) {
5853
ast := assert.New(t)
5954

6055
// The original Problem requires the binary tree constructed from array. Please refer to test cases produced at Leetcode problem https://leetcode.com/problems/maximum-level-sum-of-a-binary-tree/
61-
treeNode := InitTree(1, 7, 0, 7, -8)
62-
56+
//treeNode := InitTree(1, 7, 0, 7, -8)
57+
treeNode := &TreeNode{
58+
Val: 1,
59+
Left: &TreeNode{Val: 7, Left: &TreeNode{Val: 7, Right: &TreeNode{Val: -8}}},
60+
Right: &TreeNode{Val: 0},
61+
}
6362
var cases = []struct {
6463
name string
6564
inputs *TreeNode
6665
expect int
6766
}{
6867
{"TestCase1", treeNode, 2},
6968
}
69+
//ast.Equal(treeNode, &TreeNode{})
7070
for _, f := range SolutionFuncList {
7171
for _, c := range cases {
7272
t.Run(c.name, func(t *testing.T) {

0 commit comments

Comments
 (0)