Skip to content

Commit 21757f5

Browse files
6borisgitbook-bot
authored andcommitted
GitBook: [master] 196 pages modified
1 parent 2149fef commit 21757f5

File tree

3 files changed

+72
-54
lines changed

3 files changed

+72
-54
lines changed

docs/jzof/of004.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
description: 剑指 Offer 04. 二维数组中的查找
33
---
44

5-
# OF4. 二维数组中的查找
5+
# OF4.二维数组中的查找
66

77
## 题目描述
88

docs/jzof/of005.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
description: 剑指 Offer 05. 替换空格
33
---
44

5-
# OF5. 替换空格
5+
# OF5.替换空格
66

77
## 题目描述
88

docs/jzof/of007.md

Lines changed: 70 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -2,91 +2,109 @@
22
description: 剑指 Offer 07. 重建二叉树
33
---
44

5-
# OF6.重建二叉树
5+
# OF7.重建二叉树
66

77
## 题目描述
88

99
[题目地址](https://leetcode-cn.com/problems/zhong-jian-er-cha-shu-lcof/)
1010

1111
输入某二叉树的前序遍历和中序遍历的结果,请重建该二叉树。假设输入的前序遍历和中序遍历的结果中都不含重复的数字。
1212

13-
1413
### **示例 1:**
1514

1615
```go
1716
前序遍历 preorder = [3,9,20,15,7]
1817
中序遍历 inorder = [9,3,15,20,7]
1918

20-
3
21-
/ \
22-
9 20
23-
/ \
24-
15 7
19+
3
20+
/ \
21+
9 20
22+
/ \
23+
15 7
2524
```
2625

2726
## 题解
2827

29-
### 思路1 : 递归
28+
### 思路1 : ...
3029

3130
递归遍历
3231

3332
**算法流程:**
3433

3534
1. **复杂度分析:**
36-
2. **时间复杂度**$$O(N)$$****遍历N次,递归 N 次
37-
3. **空间复杂度**$$O(N)$$****递归 N 次,开辟 N 个栈空间
35+
2. **时间复杂度**$$O(N)$$****
36+
3. **空间复杂度**$$O(N)$$****
3837

3938
#### 代码
4039

4140
{% tabs %}
4241
{% tab title="Go" %}
4342
```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
43+
type TreeNode struct {
44+
Val int
45+
Left *TreeNode
46+
Right *TreeNode
47+
}
48+
49+
// 递归求解
50+
func buildTree(pre []int, in []int) *TreeNode {
51+
if len(pre) == 0 || len(in) == 0 {
52+
return nil
53+
}
54+
mid := search(in, pre[0])
55+
return &TreeNode{
56+
Val: pre[0],
57+
Left: buildTree(pre[1:mid+1], in[:mid+1]),
58+
Right: buildTree(pre[mid+1:], in[mid+1:]),
59+
}
60+
}
61+
func search(nodes []int, val int) int {
62+
for p, v := range nodes {
63+
if v == val {
64+
return p
65+
}
66+
}
67+
return -1
5268
}
5369
```
5470
{% endtab %}
55-
{% endtabs %}
56-
57-
### 思路1 : 多指针
58-
59-
多个指针辅助,一次遍历
60-
61-
**算法流程:**
6271

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-
}
72+
{% tab title="Python" %}
73+
```python
74+
class Solution:
75+
def buildTree(self, preorder, inorder):
76+
"""
77+
:type preorder: List[int]
78+
:type inorder: List[int]
79+
:rtype: TreeNode
80+
"""
81+
def helper(in_left = 0, in_right = len(inorder)):
82+
nonlocal pre_idx
83+
# if there is no elements to construct subtrees
84+
if in_left == in_right:
85+
return None
86+
87+
# pick up pre_idx element as a root
88+
root_val = preorder[pre_idx]
89+
root = TreeNode(root_val)
90+
91+
# root splits inorder list
92+
# into left and right subtrees
93+
index = idx_map[root_val]
94+
95+
# recursion
96+
pre_idx += 1
97+
# build left subtree
98+
root.left = helper(in_left, index)
99+
# build right subtree
100+
root.right = helper(index + 1, in_right)
101+
return root
102+
103+
# start from first preorder element
104+
pre_idx = 0
105+
# build a hashmap value -> its index
106+
idx_map = {val:idx for idx, val in enumerate(inorder)}
107+
return helper()
90108
```
91109
{% endtab %}
92110
{% endtabs %}

0 commit comments

Comments
 (0)