From 21757f5ab7650c06fa4cb39dc13d91f4818926f3 Mon Sep 17 00:00:00 2001 From: Kyle Liu Date: Thu, 24 Dec 2020 16:09:43 +0000 Subject: [PATCH] GitBook: [master] 196 pages modified --- docs/jzof/of004.md | 2 +- docs/jzof/of005.md | 2 +- docs/jzof/of007.md | 122 ++++++++++++++++++++++++++------------------- 3 files changed, 72 insertions(+), 54 deletions(-) diff --git a/docs/jzof/of004.md b/docs/jzof/of004.md index e1a9eecaa..1701a114a 100644 --- a/docs/jzof/of004.md +++ b/docs/jzof/of004.md @@ -2,7 +2,7 @@ description: 剑指 Offer 04. 二维数组中的查找 --- -# OF4. 二维数组中的查找 +# OF4.二维数组中的查找 ## 题目描述 diff --git a/docs/jzof/of005.md b/docs/jzof/of005.md index 4e68bb45c..0098111f7 100644 --- a/docs/jzof/of005.md +++ b/docs/jzof/of005.md @@ -2,7 +2,7 @@ description: 剑指 Offer 05. 替换空格 --- -# OF5. 替换空格 +# OF5.替换空格 ## 题目描述 diff --git a/docs/jzof/of007.md b/docs/jzof/of007.md index 70ccd9540..702e621ba 100644 --- a/docs/jzof/of007.md +++ b/docs/jzof/of007.md @@ -2,7 +2,7 @@ description: 剑指 Offer 07. 重建二叉树 --- -# OF6.重建二叉树 +# OF7.重建二叉树 ## 题目描述 @@ -10,83 +10,101 @@ description: 剑指 Offer 07. 重建二叉树 输入某二叉树的前序遍历和中序遍历的结果,请重建该二叉树。假设输入的前序遍历和中序遍历的结果中都不含重复的数字。 - ### **示例 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 %}