|
2 | 2 | description: 剑指 Offer 07. 重建二叉树
|
3 | 3 | ---
|
4 | 4 |
|
5 |
| -# OF6.重建二叉树 |
| 5 | +# OF7.重建二叉树 |
6 | 6 |
|
7 | 7 | ## 题目描述
|
8 | 8 |
|
9 | 9 | [题目地址](https://leetcode-cn.com/problems/zhong-jian-er-cha-shu-lcof/)
|
10 | 10 |
|
11 | 11 | 输入某二叉树的前序遍历和中序遍历的结果,请重建该二叉树。假设输入的前序遍历和中序遍历的结果中都不含重复的数字。
|
12 | 12 |
|
13 |
| - |
14 | 13 | ### **示例 1:**
|
15 | 14 |
|
16 | 15 | ```go
|
17 | 16 | 前序遍历 preorder = [3,9,20,15,7]
|
18 | 17 | 中序遍历 inorder = [9,3,15,20,7]
|
19 | 18 |
|
20 |
| -3 |
21 |
| -/ \ |
22 |
| -9 20 |
23 |
| -/ \ |
24 |
| -15 7 |
| 19 | + 3 |
| 20 | + / \ |
| 21 | + 9 20 |
| 22 | + / \ |
| 23 | + 15 7 |
25 | 24 | ```
|
26 | 25 |
|
27 | 26 | ## 题解
|
28 | 27 |
|
29 |
| -### 思路1 : 递归 |
| 28 | +### 思路1 : ... |
30 | 29 |
|
31 | 30 | 递归遍历
|
32 | 31 |
|
33 | 32 | **算法流程:**
|
34 | 33 |
|
35 | 34 | 1. **复杂度分析:**
|
36 |
| -2. **时间复杂度**$$O(N)$$**:**遍历N次,递归 N 次 |
37 |
| -3. **空间复杂度**$$O(N)$$**:**递归 N 次,开辟 N 个栈空间 |
| 35 | +2. **时间复杂度**$$O(N)$$**:** |
| 36 | +3. **空间复杂度**$$O(N)$$**:** |
38 | 37 |
|
39 | 38 | #### 代码
|
40 | 39 |
|
41 | 40 | {% tabs %}
|
42 | 41 | {% tab title="Go" %}
|
43 | 42 | ```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 |
52 | 68 | }
|
53 | 69 | ```
|
54 | 70 | {% endtab %}
|
55 |
| -{% endtabs %} |
56 |
| - |
57 |
| -### 思路1 : 多指针 |
58 |
| - |
59 |
| -多个指针辅助,一次遍历 |
60 |
| - |
61 |
| -**算法流程:** |
62 | 71 |
|
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() |
90 | 108 | ```
|
91 | 109 | {% endtab %}
|
92 | 110 | {% endtabs %}
|
|
0 commit comments