mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-11 21:10:58 +08:00
Merge pull request #1853 from Logenleedev/my-contribution
fix minor code style + 错别字
This commit is contained in:
@ -23,10 +23,12 @@
|
||||
* 111.二叉树的最小深度
|
||||
|
||||
|
||||
|
||||

|
||||
|
||||
|
||||
|
||||
|
||||
# 102.二叉树的层序遍历
|
||||
|
||||
[力扣题目链接](https://leetcode.cn/problems/binary-tree-level-order-traversal/)
|
||||
|
@ -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.从前序与中序遍历序列构造二叉树
|
||||
|
||||
|
@ -88,7 +88,7 @@ public:
|
||||
* 情况一:如果gas的总和小于cost总和,那么无论从哪里出发,一定是跑不了一圈的
|
||||
* 情况二:rest[i] = gas[i]-cost[i]为一天剩下的油,i从0开始计算累加到最后一站,如果累加没有出现负数,说明从0出发,油就没有断过,那么0就是起点。
|
||||
|
||||
* 情况三:如果累加的最小值是负数,汽车就要从非0节点出发,从后向前,看哪个节点能这个负数填平,能把这个负数填平的节点就是出发节点。
|
||||
* 情况三:如果累加的最小值是负数,汽车就要从非0节点出发,从后向前,看哪个节点能把这个负数填平,能把这个负数填平的节点就是出发节点。
|
||||
|
||||
C++代码如下:
|
||||
|
||||
|
@ -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] != ' ':
|
||||
|
@ -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()
|
||||
|
@ -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)
|
||||
# 右
|
||||
self.convertBST(root.right)
|
||||
|
||||
# 中
|
||||
# 中节点:用当前root的值加上pre的值
|
||||
self.count += root.val
|
||||
|
||||
root.val = self.count
|
||||
|
||||
# 左
|
||||
self.convertBST(root.left)
|
||||
|
||||
return root
|
||||
|
||||
def traversal(self, root: TreeNode) -> None:
|
||||
# 因为要遍历整棵树,所以递归函数不需要返回值
|
||||
# Base Case
|
||||
if not root:
|
||||
return None
|
||||
# 单层递归逻辑:中序遍历的反译 - 右中左
|
||||
self.traversal(root.right) # 右
|
||||
|
||||
# 中节点:用当前root的值加上pre的值
|
||||
root.val += self.pre.val # 中
|
||||
self.pre = root
|
||||
|
||||
self.traversal(root.left) # 左
|
||||
```
|
||||
|
||||
## Go
|
||||
|
@ -66,7 +66,7 @@ IDE那么很吃内存,打开个IDE卡半天,用VIM就很轻便了,秒开
|
||||
|
||||
## 安装
|
||||
|
||||
PowerVim的安防非常简单,我已经写好了安装脚本,只要执行以下就可以安装,而且不会影响你之前的vim配置,之前的配置都给做了备份,大家看一下脚本就知道备份在哪里了。
|
||||
PowerVim的安装非常简单,我已经写好了安装脚本,只要执行以下就可以安装,而且不会影响你之前的vim配置,之前的配置都给做了备份,大家看一下脚本就知道备份在哪里了。
|
||||
|
||||
安装过程非常简单:
|
||||
```bash
|
||||
|
Reference in New Issue
Block a user