Skip to content

Commit

Permalink
GitBook: [master] 196 pages modified
Browse files Browse the repository at this point in the history
  • Loading branch information
6boris authored and gitbook-bot committed Dec 24, 2020
1 parent 2149fef commit 21757f5
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 54 deletions.
2 changes: 1 addition & 1 deletion docs/jzof/of004.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
description: 剑指 Offer 04. 二维数组中的查找
---

# OF4. 二维数组中的查找
# OF4.二维数组中的查找

## 题目描述

Expand Down
2 changes: 1 addition & 1 deletion docs/jzof/of005.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
description: 剑指 Offer 05. 替换空格
---

# OF5. 替换空格
# OF5.替换空格

## 题目描述

Expand Down
122 changes: 70 additions & 52 deletions docs/jzof/of007.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,91 +2,109 @@
description: 剑指 Offer 07. 重建二叉树
---

# OF6.重建二叉树
# OF7.重建二叉树

## 题目描述

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

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


### **示例 1:**

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

3
/ \
9 20
/ \
15 7
3
/ \
9 20
/ \
15 7
```

## 题解

### 思路1 : 递归
### 思路1 : ...

递归遍历

**算法流程:**

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

#### 代码

{% tabs %}
{% tab title="Go" %}
```go
func reversePrint(head *ListNode) []int {
ans := make([]int, 0)
if head == nil {
return ans
}
ans = reversePrint(head.Next)
ans = append(ans, head.Val)
return ans
type TreeNode struct {
Val int
Left *TreeNode
Right *TreeNode
}

// 递归求解
func buildTree(pre []int, in []int) *TreeNode {
if len(pre) == 0 || len(in) == 0 {
return nil
}
mid := search(in, pre[0])
return &TreeNode{
Val: pre[0],
Left: buildTree(pre[1:mid+1], in[:mid+1]),
Right: buildTree(pre[mid+1:], in[mid+1:]),
}
}
func search(nodes []int, val int) int {
for p, v := range nodes {
if v == val {
return p
}
}
return -1
}
```
{% endtab %}
{% endtabs %}

### 思路1 : 多指针

多个指针辅助,一次遍历

**算法流程:**

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

#### 代码

{% tabs %}
{% tab title="Go" %}
```go
func reversePrint(head *ListNode) []int {
if head == nil {
return []int{}
}
pre, cur, next, ans := &ListNode{}, head, head.Next, []int{}
for cur != nil {
next = cur.Next
cur.Next = pre

pre = cur
cur = next
}
for pre.Next != nil {
ans = append(ans, pre.Val)
pre = pre.Next
}
return ans
}
{% tab title="Python" %}
```python
class Solution:
def buildTree(self, preorder, inorder):
"""
:type preorder: List[int]
:type inorder: List[int]
:rtype: TreeNode
"""
def helper(in_left = 0, in_right = len(inorder)):
nonlocal pre_idx
# if there is no elements to construct subtrees
if in_left == in_right:
return None

# pick up pre_idx element as a root
root_val = preorder[pre_idx]
root = TreeNode(root_val)

# root splits inorder list
# into left and right subtrees
index = idx_map[root_val]

# recursion
pre_idx += 1
# build left subtree
root.left = helper(in_left, index)
# build right subtree
root.right = helper(index + 1, in_right)
return root

# start from first preorder element
pre_idx = 0
# build a hashmap value -> its index
idx_map = {val:idx for idx, val in enumerate(inorder)}
return helper()
```
{% endtab %}
{% endtabs %}
Expand Down

0 comments on commit 21757f5

Please sign in to comment.