diff --git a/problems/0102.二叉树的层序遍历.md b/problems/0102.二叉树的层序遍历.md index 220232a2..36ae1ec6 100644 --- a/problems/0102.二叉树的层序遍历.md +++ b/problems/0102.二叉树的层序遍历.md @@ -23,10 +23,12 @@ * 111.二叉树的最小深度 + ![我要打十个](https://code-thinking.cdn.bcebos.com/gifs/%E6%88%91%E8%A6%81%E6%89%93%E5%8D%81%E4%B8%AA.gif) + # 102.二叉树的层序遍历 [力扣题目链接](https://leetcode.cn/problems/binary-tree-level-order-traversal/) diff --git a/problems/0106.从中序与后序遍历序列构造二叉树.md b/problems/0106.从中序与后序遍历序列构造二叉树.md index f8109f85..c2b2872b 100644 --- a/problems/0106.从中序与后序遍历序列构造二叉树.md +++ b/problems/0106.从中序与后序遍历序列构造二叉树.md @@ -397,6 +397,9 @@ public: }; ``` +## Python + + # 105.从前序与中序遍历序列构造二叉树 [力扣题目链接](https://leetcode.cn/problems/construct-binary-tree-from-preorder-and-inorder-traversal/) @@ -650,6 +653,37 @@ class Solution { ``` ## Python +```python +class Solution: + def buildTree(self, inorder: List[int], postorder: List[int]) -> Optional[TreeNode]: + # 第一步: 特殊情况讨论: 树为空. 或者说是递归终止条件 + if not postorder: + return + + # 第二步: 后序遍历的最后一个就是当前的中间节点 + root_val = postorder[-1] + root = TreeNode(root_val) + + # 第三步: 找切割点. + root_index = inorder.index(root_val) + + # 第四步: 切割inorder数组. 得到inorder数组的左,右半边. + left_inorder = inorder[:root_index] + right_inorder = inorder[root_index + 1:] + + # 第五步: 切割postorder数组. 得到postorder数组的左,右半边. + # ⭐️ 重点1: 中序数组大小一定跟后序数组大小是相同的. + left_postorder = postorder[:len(left_inorder)] + right_postorder = postorder[len(left_inorder): len(postorder) - 1] + + + # 第六步: 递归 + root.left = self.buildTree(left_inorder, left_postorder) + root.right = self.buildTree(right_inorder, right_postorder) + + # 第七步: 返回答案 + return root +``` 105.从前序与中序遍历序列构造二叉树 diff --git a/problems/0134.加油站.md b/problems/0134.加油站.md index ade84773..192e58a6 100644 --- a/problems/0134.加油站.md +++ b/problems/0134.加油站.md @@ -88,7 +88,7 @@ public: * 情况一:如果gas的总和小于cost总和,那么无论从哪里出发,一定是跑不了一圈的 * 情况二:rest[i] = gas[i]-cost[i]为一天剩下的油,i从0开始计算累加到最后一站,如果累加没有出现负数,说明从0出发,油就没有断过,那么0就是起点。 -* 情况三:如果累加的最小值是负数,汽车就要从非0节点出发,从后向前,看哪个节点能这个负数填平,能把这个负数填平的节点就是出发节点。 +* 情况三:如果累加的最小值是负数,汽车就要从非0节点出发,从后向前,看哪个节点能把这个负数填平,能把这个负数填平的节点就是出发节点。 C++代码如下: diff --git a/problems/0151.翻转字符串里的单词.md b/problems/0151.翻转字符串里的单词.md index a12d8f76..dc12ae23 100644 --- a/problems/0151.翻转字符串里的单词.md +++ b/problems/0151.翻转字符串里的单词.md @@ -442,7 +442,7 @@ class Solution: while left <= right and s[left] == ' ': #去除开头的空格 left += 1 while left <= right and s[right] == ' ': #去除结尾的空格 - right = right-1 + right -= 1 tmp = [] while left <= right: #去除单词中间多余的空格 if s[left] != ' ': diff --git a/problems/0455.分发饼干.md b/problems/0455.分发饼干.md index 8b05735c..2471cb68 100644 --- a/problems/0455.分发饼干.md +++ b/problems/0455.分发饼干.md @@ -173,7 +173,7 @@ class Solution { ### Python ```python class Solution: - # 思路1:优先考虑胃饼干 + # 思路1:优先考虑小胃口 def findContentChildren(self, g: List[int], s: List[int]) -> int: g.sort() s.sort() @@ -185,7 +185,7 @@ class Solution: ``` ```python class Solution: - # 思路2:优先考虑胃口 + # 思路2:优先考虑大胃口 def findContentChildren(self, g: List[int], s: List[int]) -> int: g.sort() s.sort() diff --git a/problems/0538.把二叉搜索树转换为累加树.md b/problems/0538.把二叉搜索树转换为累加树.md index 3cb9d3db..8d4b97f1 100644 --- a/problems/0538.把二叉搜索树转换为累加树.md +++ b/problems/0538.把二叉搜索树转换为累加树.md @@ -209,29 +209,28 @@ class Solution { # self.right = right class Solution: def __init__(self): - self.pre = TreeNode() + self.count = 0 def convertBST(self, root: Optional[TreeNode]) -> Optional[TreeNode]: + if root == None: + return ''' 倒序累加替换: - [2, 5, 13] -> [[2]+[1]+[0], [2]+[1], [2]] -> [20, 18, 13] ''' - self.traversal(root) - return root - - def traversal(self, root: TreeNode) -> None: - # 因为要遍历整棵树,所以递归函数不需要返回值 - # Base Case - if not root: - return None - # 单层递归逻辑:中序遍历的反译 - 右中左 - self.traversal(root.right) # 右 + # 右 + self.convertBST(root.right) + # 中 # 中节点:用当前root的值加上pre的值 - root.val += self.pre.val # 中 - self.pre = root + self.count += root.val - self.traversal(root.left) # 左 + root.val = self.count + + # 左 + self.convertBST(root.left) + + return root + ``` ## Go diff --git a/problems/前序/vim.md b/problems/前序/vim.md index f84e70ac..5c5910d2 100644 --- a/problems/前序/vim.md +++ b/problems/前序/vim.md @@ -66,7 +66,7 @@ IDE那么很吃内存,打开个IDE卡半天,用VIM就很轻便了,秒开 ## 安装 -PowerVim的安防非常简单,我已经写好了安装脚本,只要执行以下就可以安装,而且不会影响你之前的vim配置,之前的配置都给做了备份,大家看一下脚本就知道备份在哪里了。 +PowerVim的安装非常简单,我已经写好了安装脚本,只要执行以下就可以安装,而且不会影响你之前的vim配置,之前的配置都给做了备份,大家看一下脚本就知道备份在哪里了。 安装过程非常简单: ```bash