) -> i32 {
+ v.insert(0, 0); // 便于使第一个元素能够有左侧<=它的值
+ v.push(0); // 便于在结束处理最后一个元素后清空残留在栈中的值
+ let mut res = 0;
+ let mut stack = vec![]; // 递增的栈
+ for (idx, &e) in v.iter().enumerate() {
+ while !stack.is_empty() && v[*stack.last().unwrap()] > e {
+ let pos = stack.pop().unwrap();
+ let prev_pos = *stack.last().unwrap();
+ let s = (idx - prev_pos - 1) as i32 * v[pos];
+ res = res.max(s);
+ }
+ stack.push(idx);
+ }
+ res
+ }
+}
+```
+
diff --git a/problems/0090.子集II.md b/problems/0090.子集II.md
index 3238ee52..13080cd9 100644
--- a/problems/0090.子集II.md
+++ b/problems/0090.子集II.md
@@ -4,7 +4,6 @@
参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!
-
# 90.子集II
[力扣题目链接](https://leetcode.cn/problems/subsets-ii/)
@@ -25,9 +24,9 @@
[]
]
-# 算法公开课
+## 算法公开课
-**《代码随想录》算法视频公开课:[回溯算法解决子集问题,如何去重?| LeetCode:90.子集II](https://www.bilibili.com/video/BV1vm4y1F71J/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
+**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[回溯算法解决子集问题,如何去重?| LeetCode:90.子集II](https://www.bilibili.com/video/BV1vm4y1F71J/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
## 思路
@@ -240,7 +239,7 @@ class Solution {
-#### Python3
+### Python3
回溯 利用used数组去重
```python
@@ -646,3 +645,4 @@ object Solution {
+
diff --git a/problems/0093.复原IP地址.md b/problems/0093.复原IP地址.md
index 55e57dde..59cd92da 100644
--- a/problems/0093.复原IP地址.md
+++ b/problems/0093.复原IP地址.md
@@ -40,16 +40,12 @@
* 0 <= s.length <= 3000
* s 仅由数字组成
-# 算法公开课
+## 算法公开课
-**《代码随想录》算法视频公开课:[93.复原IP地址](https://www.bilibili.com/video/BV1XP4y1U73i/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
-
-# 算法公开课
-
-**《代码随想录》算法视频公开课:[回溯算法如何分割字符串并判断是合法IP?| LeetCode:93.复原IP地址](https://www.bilibili.com/video/BV1XP4y1U73i/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
+**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[回溯算法如何分割字符串并判断是合法IP?| LeetCode:93.复原IP地址](https://www.bilibili.com/video/BV1XP4y1U73i/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
-# 思路
+## 思路
做这道题目之前,最好先把[131.分割回文串](https://programmercarl.com/0131.分割回文串.html)这个做了。
@@ -63,7 +59,7 @@

-## 回溯三部曲
+### 回溯三部曲
* 递归参数
@@ -134,7 +130,7 @@ for (int i = startIndex; i < s.size(); i++) {
}
```
-## 判断子串是否合法
+### 判断子串是否合法
最后就是在写一个判断段位是否是有效段位了。
@@ -169,8 +165,6 @@ bool isValid(const string& s, int start, int end) {
}
```
-## C++代码
-
根据[关于回溯算法,你该了解这些!](https://programmercarl.com/回溯算法理论基础.html)给出的回溯算法模板:
@@ -247,7 +241,7 @@ public:
* 时间复杂度: O(3^4),IP地址最多包含4个数字,每个数字最多有3种可能的分割方式,则搜索树的最大深度为4,每个节点最多有3个子节点。
* 空间复杂度: O(n)
-# 总结
+## 总结
在[131.分割回文串](https://programmercarl.com/0131.分割回文串.html)中我列举的分割字符串的难点,本题都覆盖了。
@@ -259,9 +253,9 @@ public:
-# 其他语言版本
+## 其他语言版本
-## java
+### Java
```java
class Solution {
@@ -402,7 +396,7 @@ class Solution {
```
-## python
+### Python
回溯(版本一)
```python
@@ -478,9 +472,7 @@ class Solution:
```
-
-
-## Go
+### Go
```go
var (
@@ -517,7 +509,7 @@ func dfs(s string, start int) {
}
```
-## JavaScript
+### JavaScript
```js
/**
@@ -547,7 +539,7 @@ var restoreIpAddresses = function(s) {
};
```
-## TypeScript
+### TypeScript
```typescript
function isValidIpSegment(str: string): boolean {
@@ -586,7 +578,7 @@ function restoreIpAddresses(s: string): string[] {
};
```
-## Rust
+### Rust
```Rust
impl Solution {
@@ -643,7 +635,7 @@ impl Solution {
}
```
-## C
+### C
```c
//记录结果
char** result;
@@ -719,7 +711,7 @@ char ** restoreIpAddresses(char * s, int* returnSize){
}
```
-## Swift
+### Swift
```swift
// 判断区间段是否合法
@@ -766,7 +758,7 @@ func restoreIpAddresses(_ s: String) -> [String] {
}
```
-## Scala
+### Scala
```scala
object Solution {
@@ -813,3 +805,4 @@ object Solution {
+
diff --git a/problems/0096.不同的二叉搜索树.md b/problems/0096.不同的二叉搜索树.md
index 368a5747..8d58cc5a 100644
--- a/problems/0096.不同的二叉搜索树.md
+++ b/problems/0096.不同的二叉搜索树.md
@@ -16,9 +16,9 @@

-# 算法公开课
+## 算法公开课
-**《代码随想录》算法视频公开课:[动态规划找到子状态之间的关系很重要!| LeetCode:96.不同的二叉搜索树](https://www.bilibili.com/video/BV1eK411o7QA/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
+**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[动态规划找到子状态之间的关系很重要!| LeetCode:96.不同的二叉搜索树](https://www.bilibili.com/video/BV1eK411o7QA/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
## 思路
diff --git a/problems/0098.验证二叉搜索树.md b/problems/0098.验证二叉搜索树.md
index 95b657a5..a48ec065 100644
--- a/problems/0098.验证二叉搜索树.md
+++ b/problems/0098.验证二叉搜索树.md
@@ -20,18 +20,18 @@

-# 视频讲解
+## 算法公开课
-**《代码随想录》算法视频公开课:[你对二叉搜索树了解的还不够! | LeetCode:98.验证二叉搜索树](https://www.bilibili.com/video/BV18P411n7Q4),相信结合视频在看本篇题解,更有助于大家对本题的理解**。
+**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[你对二叉搜索树了解的还不够! | LeetCode:98.验证二叉搜索树](https://www.bilibili.com/video/BV18P411n7Q4),相信结合视频在看本篇题解,更有助于大家对本题的理解**。
-# 思路
+## 思路
要知道中序遍历下,输出的二叉搜索树节点的数值是有序序列。
有了这个特性,**验证二叉搜索树,就相当于变成了判断一个序列是不是递增的了。**
-## 递归法
+### 递归法
可以递归中序遍历将二叉搜索树转变成一个数组,代码如下:
@@ -211,7 +211,7 @@ public:
最后这份代码看上去整洁一些,思路也清晰。
-## 迭代法
+### 迭代法
可以用迭代法模拟二叉树中序遍历,对前中后序迭代法生疏的同学可以看这两篇[二叉树:听说递归能做的,栈也能做!](https://programmercarl.com/二叉树的迭代遍历.html),[二叉树:前中后序迭代方式统一写法](https://programmercarl.com/二叉树的统一迭代法.html)
@@ -245,7 +245,7 @@ public:
在[二叉树:二叉搜索树登场!](https://programmercarl.com/0700.二叉搜索树中的搜索.html)中我们分明写出了痛哭流涕的简洁迭代法,怎么在这里不行了呢,因为本题是要验证二叉搜索树啊。
-# 总结
+## 总结
这道题目是一个简单题,但对于没接触过的同学还是有难度的。
@@ -254,10 +254,10 @@ public:
只要把基本类型的题目都做过,总结过之后,思路自然就开阔了。
-# 其他语言版本
+## 其他语言版本
-## Java
+### Java
```Java
//使用統一迭代法
@@ -369,7 +369,7 @@ class Solution {
}
```
-## Python
+### Python
递归法(版本一)利用中序递增性质,转换成数组
```python
@@ -479,7 +479,7 @@ class Solution:
```
-## Go
+### Go
```Go
func isValidBST(root *TreeNode) bool {
@@ -526,7 +526,7 @@ func isValidBST(root *TreeNode) bool {
}
```
-## JavaScript
+### JavaScript
辅助数组解决
@@ -595,7 +595,7 @@ var isValidBST = function (root) {
};
```
-## TypeScript
+### TypeScript
> 辅助数组解决:
@@ -637,7 +637,7 @@ function isValidBST(root: TreeNode | null): boolean {
};
```
-## Scala
+### Scala
辅助数组解决:
```scala
@@ -682,7 +682,7 @@ object Solution {
}
```
-## rust
+### Rust
递归:
@@ -735,3 +735,4 @@ impl Solution {
+
diff --git a/problems/0100.相同的树.md b/problems/0100.相同的树.md
index 96acacf6..56a6c884 100644
--- a/problems/0100.相同的树.md
+++ b/problems/0100.相同的树.md
@@ -19,7 +19,7 @@

-# 思路
+## 思路
在[101.对称二叉树](https://programmercarl.com/0101.对称二叉树.html)中,我们讲到对于二叉树是否对称,要比较的是根节点的左子树与右子树是不是相互翻转的,理解这一点就知道了**其实我们要比较的是两个树(这两个树是根节点的左右子树)**,所以在递归遍历的过程中,也是要同时遍历两棵树。
@@ -115,7 +115,7 @@ public:
当然我可以把如上代码整理如下:
-## 递归
+### 递归
```CPP
class Solution {
@@ -134,7 +134,7 @@ public:
};
```
-## 迭代法
+### 迭代法
```CPP
class Solution {
@@ -166,9 +166,9 @@ public:
};
```
-# 其他语言版本
+## 其他语言版本
-Java:
+### Java:
```java
// 递归法
@@ -205,7 +205,8 @@ class Solution {
}
}
```
-Python:
+### Python:
+
```python
# 递归法
class Solution:
@@ -236,7 +237,8 @@ class Solution:
que.append(rightNode.right)
return True
```
-Go:
+### Go:
+
> 递归法
```go
func isSameTree(p *TreeNode, q *TreeNode) bool {
@@ -258,7 +260,7 @@ func isSameTree(p *TreeNode, q *TreeNode) bool {
}
```
-JavaScript:
+### JavaScript:
> 递归法
@@ -296,7 +298,7 @@ var isSameTree = (p, q) => {
};
```
-TypeScript:
+### TypeScript:
> 递归法-先序遍历
@@ -341,3 +343,4 @@ function isSameTree(p: TreeNode | null, q: TreeNode | null): boolean {
+
diff --git a/problems/0101.对称二叉树.md b/problems/0101.对称二叉树.md
index 4418c62d..36b39740 100644
--- a/problems/0101.对称二叉树.md
+++ b/problems/0101.对称二叉树.md
@@ -13,9 +13,11 @@

-# 思路
+## 算法公开课
-《代码随想录》算法视频公开课:[同时操作两个二叉树 | LeetCode:101. 对称二叉树](https://www.bilibili.com/video/BV1ue4y1Y7Mf),相信结合视频在看本篇题解,更有助于大家对本题的理解。
+**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[同时操作两个二叉树 | LeetCode:101. 对称二叉树](https://www.bilibili.com/video/BV1ue4y1Y7Mf), 相信结合视频再看本篇题解,更有助于大家对本题的理解**。
+
+## 思路
**首先想清楚,判断对称二叉树要比较的是哪两个节点,要比较的可不是左右节点!**
@@ -41,7 +43,7 @@
那么我们先来看看递归法的代码应该怎么写。
-## 递归法
+### 递归法
递归三部曲
@@ -159,13 +161,13 @@ public:
**所以建议大家做题的时候,一定要想清楚逻辑,每一步做什么。把题目所有情况想到位,相应的代码写出来之后,再去追求简洁代码的效果。**
-## 迭代法
+### 迭代法
这道题目我们也可以使用迭代法,但要注意,这里的迭代法可不是前中后序的迭代写法,因为本题的本质是判断两个树是否是相互翻转的,其实已经不是所谓二叉树遍历的前中后序的关系了。
这里我们可以使用队列来比较两个树(根节点的左右子树)是否相互翻转,(**注意这不是层序遍历**)
-### 使用队列
+#### 使用队列
通过队列来判断根节点的左子树和右子树的内侧和外侧是否相等,如动画所示:
@@ -207,7 +209,7 @@ public:
};
```
-### 使用栈
+#### 使用栈
细心的话,其实可以发现,这个迭代法,其实是把左右两个子树要比较的元素顺序放进一个容器,然后成对成对的取出来进行比较,那么其实使用栈也是可以的。
@@ -254,12 +256,12 @@ public:
这两道题目基本和本题是一样的,只要稍加修改就可以AC。
-* 100.相同的树
-* 572.另一个树的子树
+* [100.相同的树](https://leetcode.cn/problems/same-tree/)
+* [572.另一个树的子树](https://leetcode.cn/problems/subtree-of-another-tree/)
-# 其他语言版本
+## 其他语言版本
-Java
+### Java:
```Java
/**
@@ -364,7 +366,7 @@ Java
```
-Python
+### Python:
递归法:
```python
@@ -470,7 +472,8 @@ class Solution:
return True
```
-Go
+### Go:
+
```go
/**
* Definition for a binary tree node.
@@ -521,8 +524,7 @@ func isSymmetric(root *TreeNode) bool {
}
```
-
-JavaScript
+### JavaScript:
递归判断是否为对称二叉树:
```javascript
@@ -610,7 +612,7 @@ var isSymmetric = function(root) {
};
```
-TypeScript:
+### TypeScript:
> 递归法
@@ -679,7 +681,7 @@ function isSymmetric(root: TreeNode | null): boolean {
};
```
-Swift:
+### Swift:
> 递归
```swift
@@ -761,7 +763,7 @@ func isSymmetric3(_ root: TreeNode?) -> Bool {
}
```
-Scala
+### Scala:
> 递归:
```scala
@@ -835,7 +837,7 @@ object Solution {
}
```
-## Rust
+### Rust:
递归:
```rust
@@ -900,3 +902,4 @@ impl Solution {
+
diff --git a/problems/0102.二叉树的层序遍历.md b/problems/0102.二叉树的层序遍历.md
index c2ad9508..ce9a247c 100644
--- a/problems/0102.二叉树的层序遍历.md
+++ b/problems/0102.二叉树的层序遍历.md
@@ -8,21 +8,23 @@
# 二叉树层序遍历登场!
-《代码随想录》算法视频公开课:[讲透二叉树的层序遍历 | 广度优先搜索 | LeetCode:102.二叉树的层序遍历](https://www.bilibili.com/video/BV1GY4y1u7b2),相信结合视频在看本篇题解,更有助于大家对本题的理解。
+## 算法公开课
+
+**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[讲透二叉树的层序遍历 | 广度优先搜索 | LeetCode:102.二叉树的层序遍历](https://www.bilibili.com/video/BV1GY4y1u7b2),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
学会二叉树的层序遍历,可以一口气打完以下十题:
-* 102.二叉树的层序遍历
-* 107.二叉树的层次遍历II
-* 199.二叉树的右视图
-* 637.二叉树的层平均值
-* 429.N叉树的层序遍历
-* 515.在每个树行中找最大值
-* 116.填充每个节点的下一个右侧节点指针
-* 117.填充每个节点的下一个右侧节点指针II
-* 104.二叉树的最大深度
-* 111.二叉树的最小深度
+* [102.二叉树的层序遍历](https://leetcode.cn/problems/binary-tree-level-order-traversal/)
+* [107.二叉树的层次遍历II](https://leetcode.cn/problems/binary-tree-level-order-traversal-ii/)
+* [199.二叉树的右视图](https://leetcode.cn/problems/binary-tree-right-side-view/)
+* [637.二叉树的层平均值](https://leetcode.cn/problems/binary-tree-right-side-view/)
+* [429.N叉树的层序遍历](https://leetcode.cn/problems/n-ary-tree-level-order-traversal/)
+* [515.在每个树行中找最大值](https://leetcode.cn/problems/find-largest-value-in-each-tree-row/)
+* [116.填充每个节点的下一个右侧节点指针](https://leetcode.cn/problems/populating-next-right-pointers-in-each-node/)
+* [117.填充每个节点的下一个右侧节点指针II](https://leetcode.cn/problems/populating-next-right-pointers-in-each-node-ii/)
+* [104.二叉树的最大深度](https://leetcode.cn/problems/maximum-depth-of-binary-tree/)
+* [111.二叉树的最小深度](https://leetcode.cn/problems/minimum-depth-of-binary-tree/)
@@ -31,7 +33,7 @@
-# 102.二叉树的层序遍历
+## 102.二叉树的层序遍历
[力扣题目链接](https://leetcode.cn/problems/binary-tree-level-order-traversal/)
@@ -39,7 +41,7 @@

-思路:
+### 思路
我们之前讲过了三篇关于二叉树的深度优先遍历的文章:
@@ -63,7 +65,7 @@
代码如下:**这份代码也可以作为二叉树层序遍历的模板,打十个就靠它了**。
-C++代码:
+c++代码如下:
```CPP
class Solution {
@@ -111,7 +113,9 @@ public:
};
```
-java:
+### 其他语言版本
+
+#### Java:
```Java
// 102.二叉树的层序遍历
@@ -167,7 +171,7 @@ class Solution {
}
```
-python3代码:
+#### Python:
```python
@@ -219,12 +223,9 @@ class Solution:
self.helper(node.left, level + 1, levels)
self.helper(node.right, level + 1, levels)
-
```
-
-
-go:
+#### Go:
```go
/**
@@ -320,7 +321,7 @@ func levelOrder(root *TreeNode) (res [][]int) {
}
```
-javascript代码:
+#### Javascript:
```javascript
var levelOrder = function(root) {
@@ -350,7 +351,7 @@ var levelOrder = function(root) {
```
-TypeScript:
+#### TypeScript:
```typescript
function levelOrder(root: TreeNode | null): number[][] {
@@ -377,7 +378,7 @@ function levelOrder(root: TreeNode | null): number[][] {
};
```
-Swift:
+#### Swift:
```swift
func levelOrder(_ root: TreeNode?) -> [[Int]] {
@@ -403,7 +404,7 @@ func levelOrder(_ root: TreeNode?) -> [[Int]] {
}
```
-Scala:
+#### Scala:
```scala
// 102.二叉树的层序遍历
@@ -430,7 +431,7 @@ object Solution {
}
```
-Rust:
+#### Rust:
```rust
use std::cell::RefCell;
@@ -466,7 +467,7 @@ impl Solution {
**此时我们就掌握了二叉树的层序遍历了,那么如下九道力扣上的题目,只需要修改模板的两三行代码(不能再多了),便可打倒!**
-# 107.二叉树的层次遍历 II
+## 107.二叉树的层次遍历 II
[力扣题目链接](https://leetcode.cn/problems/binary-tree-level-order-traversal-ii/)
@@ -474,7 +475,7 @@ impl Solution {

-思路:
+### 思路
相对于102.二叉树的层序遍历,就是最后把result数组反转一下就可以了。
@@ -506,7 +507,9 @@ public:
};
```
-python代码:
+### 其他语言版本
+
+#### Python:
```python
class Solution:
@@ -537,7 +540,7 @@ class Solution:
return result[::-1]
```
-Java:
+#### Java:
```java
// 107. 二叉树的层序遍历 II
@@ -618,12 +621,10 @@ class Solution {
return ans;
}
-}
+
```
-
-
-go:
+#### Go:
```GO
/**
@@ -662,7 +663,7 @@ func levelOrderBottom(root *TreeNode) [][]int {
}
```
-javascript代码
+#### Javascript:
```javascript
var levelOrderBottom = function(root) {
@@ -688,7 +689,7 @@ var levelOrderBottom = function(root) {
};
```
-TypeScript:
+#### TypeScript:
```typescript
function levelOrderBottom(root: TreeNode | null): number[][] {
@@ -711,7 +712,7 @@ function levelOrderBottom(root: TreeNode | null): number[][] {
};
```
-Swift:
+#### Swift:
```swift
func levelOrderBottom(_ root: TreeNode?) -> [[Int]] {
@@ -737,8 +738,7 @@ func levelOrderBottom(_ root: TreeNode?) -> [[Int]] {
}
```
-
-Scala:
+#### Scala:
```scala
// 107.二叉树的层次遍历II
@@ -764,7 +764,10 @@ object Solution {
res.reverse.toList
}
-Rust:
+
+```
+
+#### Rust:
```rust
use std::cell::RefCell;
@@ -796,7 +799,7 @@ impl Solution {
}
```
-# 199.二叉树的右视图
+## 199.二叉树的右视图
[力扣题目链接](https://leetcode.cn/problems/binary-tree-right-side-view/)
@@ -804,7 +807,7 @@ impl Solution {

-思路:
+### 思路
层序遍历的时候,判断是否遍历到单层的最后面的元素,如果是,就放进result数组中,随后返回result就可以了。
@@ -832,7 +835,9 @@ public:
};
```
-python代码:
+### 其他语言版本
+
+#### Python:
```python
# Definition for a binary tree node.
@@ -866,8 +871,7 @@ class Solution:
return right_view
```
-
-Java:
+#### Java:
```java
// 199.二叉树的右视图
@@ -911,7 +915,7 @@ public class N0199 {
}
```
-go:
+#### Go:
```GO
/**
@@ -945,8 +949,7 @@ func rightSideView(root *TreeNode) []int {
}
```
-
-javascript代码:
+#### Javascript:
```javascript
var rightSideView = function(root) {
@@ -972,7 +975,7 @@ var rightSideView = function(root) {
};
```
-TypeScript:
+#### TypeScript:
```typescript
function rightSideView(root: TreeNode | null): number[] {
@@ -992,7 +995,7 @@ function rightSideView(root: TreeNode | null): number[] {
};
```
-Swift:
+#### Swift:
```swift
func rightSideView(_ root: TreeNode?) -> [Int] {
@@ -1017,7 +1020,7 @@ func rightSideView(_ root: TreeNode?) -> [Int] {
}
```
-Scala:
+#### Scala:
```scala
// 199.二叉树的右视图
@@ -1043,7 +1046,7 @@ object Solution {
}
```
-rust:
+#### Rust:
```rust
use std::cell::RefCell;
@@ -1076,7 +1079,7 @@ impl Solution {
}
```
-# 637.二叉树的层平均值
+## 637.二叉树的层平均值
[力扣题目链接](https://leetcode.cn/problems/average-of-levels-in-binary-tree/)
@@ -1084,7 +1087,7 @@ impl Solution {

-思路:
+### 思路
本题就是层序遍历的时候把一层求个总和在取一个均值。
@@ -1115,7 +1118,9 @@ public:
```
-python代码:
+### 其他语言版本
+
+#### Python:
```python
class Solution:
@@ -1155,7 +1160,7 @@ class Solution:
return averages
```
-java:
+#### Java:
```java
// 637. 二叉树的层平均值
@@ -1197,7 +1202,7 @@ public class N0637 {
}
```
-go:
+#### Go:
```GO
/**
@@ -1235,7 +1240,7 @@ func averageOfLevels(root *TreeNode) []float64 {
}
```
-javascript代码:
+#### Javascript:
```javascript
var averageOfLevels = function(root) {
@@ -1262,7 +1267,7 @@ var averageOfLevels = function(root) {
};
```
-TypeScript:
+#### TypeScript:
```typescript
function averageOfLevels(root: TreeNode | null): number[] {
@@ -1286,7 +1291,7 @@ function averageOfLevels(root: TreeNode | null): number[] {
};
```
-Swift:
+#### Swift:
```swift
func averageOfLevels(_ root: TreeNode?) -> [Double] {
@@ -1313,7 +1318,7 @@ func averageOfLevels(_ root: TreeNode?) -> [Double] {
}
```
-Scala:
+#### Scala:
```scala
// 637.二叉树的层平均值
@@ -1339,7 +1344,7 @@ object Solution {
}
```
-rust:
+#### Rust:
```rust
use std::cell::RefCell;
@@ -1372,7 +1377,7 @@ impl Solution {
}
```
-# 429.N叉树的层序遍历
+## 429.N叉树的层序遍历
[力扣题目链接](https://leetcode.cn/problems/n-ary-tree-level-order-traversal/)
@@ -1390,8 +1395,7 @@ impl Solution {
[5,6]
]
-
-思路:
+### 思路
这道题依旧是模板题,只不过一个节点有多个孩子了
@@ -1423,7 +1427,9 @@ public:
};
```
-python代码:
+### 其他语言版本
+
+#### Python:
```python
"""
@@ -1475,7 +1481,7 @@ class Solution:
return result
```
-java:
+#### Java:
```java
// 429. N 叉树的层序遍历
@@ -1535,8 +1541,7 @@ public class N0429 {
}
```
-
-go:
+#### Go:
```GO
/**
@@ -1567,7 +1572,7 @@ func levelOrder(root *Node) [][]int {
}
```
-JavaScript代码:
+#### JavaScript:
```JavaScript
var levelOrder = function(root) {
@@ -1596,7 +1601,7 @@ var levelOrder = function(root) {
};
```
-TypeScript:
+#### TypeScript:
```typescript
function levelOrder(root: Node | null): number[][] {
@@ -1618,7 +1623,7 @@ function levelOrder(root: Node | null): number[][] {
};
```
-Swift:
+#### Swift:
```swift
func levelOrder(_ root: Node?) -> [[Int]] {
@@ -1643,7 +1648,7 @@ func levelOrder(_ root: Node?) -> [[Int]] {
}
```
-Scala:
+#### Scala:
```scala
// 429.N叉树的层序遍历
@@ -1672,7 +1677,7 @@ object Solution {
}
```
-rust:
+#### Rust:
```rust
pub struct Solution;
@@ -1720,7 +1725,7 @@ impl Solution {
}
```
-# 515.在每个树行中找最大值
+## 515.在每个树行中找最大值
[力扣题目链接](https://leetcode.cn/problems/find-largest-value-in-each-tree-row/)
@@ -1728,7 +1733,7 @@ impl Solution {

-思路:
+### 思路
层序遍历,取每一层的最大值
@@ -1758,7 +1763,9 @@ public:
};
```
-python代码:
+### 其他语言版本
+
+#### Python:
```python
# Definition for a binary tree node.
@@ -1794,7 +1801,7 @@ class Solution:
return result
```
-java代码:
+#### Java:
```java
class Solution {
@@ -1820,7 +1827,7 @@ class Solution {
}
```
-go:
+#### Go:
```GO
/**
@@ -1864,7 +1871,7 @@ func max(x, y int) int {
}
```
-javascript代码:
+#### Javascript:
```javascript
var largestValues = function(root) {
@@ -1890,7 +1897,7 @@ var largestValues = function(root) {
};
```
-TypeScript:
+#### TypeScript:
```typescript
function largestValues(root: TreeNode | null): number[] {
@@ -1916,7 +1923,7 @@ function largestValues(root: TreeNode | null): number[] {
};
```
-Swift:
+#### Swift:
```swift
func largestValues(_ root: TreeNode?) -> [Int] {
@@ -1943,7 +1950,7 @@ func largestValues(_ root: TreeNode?) -> [Int] {
}
```
-Scala:
+#### Scala:
```scala
// 515.在每个树行中找最大值
@@ -1970,7 +1977,7 @@ object Solution {
}
```
-rust:
+#### Rust:
```rust
use std::cell::RefCell;
@@ -2002,7 +2009,7 @@ impl Solution {
}
```
-# 116.填充每个节点的下一个右侧节点指针
+## 116.填充每个节点的下一个右侧节点指针
[力扣题目链接](https://leetcode.cn/problems/populating-next-right-pointers-in-each-node/)
@@ -2024,7 +2031,7 @@ struct Node {

-思路:
+### 思路
本题依然是层序遍历,只不过在单层遍历的时候记录一下本层的头部节点,然后在遍历的时候让前一个节点指向本节点就可以了
@@ -2063,7 +2070,9 @@ public:
};
```
-java代码:
+### 其他语言版本
+
+#### Java:
```java
class Solution {
@@ -2093,7 +2102,7 @@ class Solution {
}
```
-python代码:
+#### Python:
```python
"""
@@ -2133,7 +2142,7 @@ class Solution:
return root
```
-go:
+#### Go:
```GO
/**
@@ -2173,7 +2182,7 @@ func connect(root *Node) *Node {
```
-JavaScript:
+#### JavaScript:
```javascript
/**
@@ -2209,7 +2218,7 @@ var connect = function(root) {
```
-TypeScript:
+#### TypeScript:
```typescript
function connect(root: Node | null): Node | null {
@@ -2234,7 +2243,7 @@ function connect(root: Node | null): Node | null {
};
```
-Swift:
+#### Swift:
```swift
func connect(_ root: Node?) -> Node? {
@@ -2266,7 +2275,7 @@ func connect(_ root: Node?) -> Node? {
}
```
-Scala:
+#### Scala:
```scala
// 116.填充每个节点的下一个右侧节点指针
@@ -2297,11 +2306,11 @@ object Solution {
}
```
-# 117.填充每个节点的下一个右侧节点指针II
+## 117.填充每个节点的下一个右侧节点指针II
[力扣题目链接](https://leetcode.cn/problems/populating-next-right-pointers-in-each-node-ii/)
-思路:
+### 思路
这道题目说是二叉树,但116题目说是完整二叉树,其实没有任何差别,一样的代码一样的逻辑一样的味道
@@ -2339,7 +2348,9 @@ public:
};
```
-Java 代码:
+### 其他语言版本
+
+#### Java:
```java
// 二叉树之层次遍历
@@ -2377,7 +2388,7 @@ class Solution {
}
```
-python代码:
+#### Python:
```python
# 层序遍历解法
@@ -2420,7 +2431,7 @@ class Solution:
```
-go:
+#### Go:
```GO
/**
@@ -2459,7 +2470,7 @@ func connect(root *Node) *Node {
}
```
-JavaScript:
+#### JavaScript:
```javascript
/**
@@ -2494,7 +2505,7 @@ var connect = function(root) {
};
```
-TypeScript:
+#### TypeScript:
```typescript
function connect(root: Node | null): Node | null {
@@ -2519,7 +2530,7 @@ function connect(root: Node | null): Node | null {
};
```
-Swift:
+#### Swift:
```swift
func connect(_ root: Node?) -> Node? {
@@ -2551,7 +2562,7 @@ func connect(_ root: Node?) -> Node? {
}
```
-Scala:
+#### Scala:
```scala
// 117.填充每个节点的下一个右侧节点指针II
@@ -2582,7 +2593,7 @@ object Solution {
}
```
-# 104.二叉树的最大深度
+## 104.二叉树的最大深度
[力扣题目链接](https://leetcode.cn/problems/maximum-depth-of-binary-tree/)
@@ -2600,7 +2611,7 @@ object Solution {
返回它的最大深度 3 。
-思路:
+### 思路
使用迭代法的话,使用层序遍历是最为合适的,因为最大的深度就是二叉树的层数,和层序遍历的方式极其吻合。
@@ -2635,7 +2646,9 @@ public:
};
```
-Java:
+### 其他语言版本
+
+#### Java:
```Java
class Solution {
@@ -2661,7 +2674,7 @@ class Solution {
}
```
-Python:
+#### Python:
```python 3
# Definition for a binary tree node.
@@ -2691,7 +2704,7 @@ class Solution:
```
-Go:
+#### Go:
```go
/**
@@ -2726,7 +2739,7 @@ func maxDepth(root *TreeNode) int {
}
```
-JavaScript:
+#### JavaScript:
```javascript
/**
@@ -2759,7 +2772,7 @@ var maxDepth = function(root) {
};
```
-TypeScript:
+#### TypeScript:
```typescript
function maxDepth(root: TreeNode | null): number {
@@ -2779,7 +2792,7 @@ function maxDepth(root: TreeNode | null): number {
};
```
-Swift:
+#### Swift:
```swift
func maxDepth(_ root: TreeNode?) -> Int {
@@ -2804,7 +2817,7 @@ func maxDepth(_ root: TreeNode?) -> Int {
}
```
-Scala:
+#### Scala:
```scala
// 104.二叉树的最大深度
@@ -2829,7 +2842,7 @@ object Solution {
}
```
-rust:
+#### Rust:
```rust
use std::cell::RefCell;
@@ -2859,10 +2872,12 @@ impl Solution {
}
```
-# 111.二叉树的最小深度
+## 111.二叉树的最小深度
[力扣题目链接](https://leetcode.cn/problems/minimum-depth-of-binary-tree/)
+### 思路
+
相对于 104.二叉树的最大深度 ,本题还也可以使用层序遍历的方式来解决,思路是一样的。
**需要注意的是,只有当左右孩子都为空的时候,才说明遍历的最低点了。如果其中一个孩子为空则不是最低点**
@@ -2895,7 +2910,9 @@ public:
};
```
-Java:
+### 其他语言版本
+
+#### Java:
```java
class Solution {
@@ -2925,9 +2942,7 @@ class Solution {
}
```
-
-
-Python 3:
+#### Python:
```python 3
# Definition for a binary tree node.
@@ -2960,7 +2975,7 @@ class Solution:
return depth
```
-Go:
+#### Go:
```go
/**
@@ -2999,7 +3014,7 @@ func minDepth(root *TreeNode) int {
}
```
-JavaScript:
+#### JavaScript:
```javascript
/**
@@ -3035,7 +3050,7 @@ var minDepth = function(root) {
};
```
-TypeScript:
+#### TypeScript:
```typescript
function minDepth(root: TreeNode | null): number {
@@ -3056,7 +3071,7 @@ function minDepth(root: TreeNode | null): number {
};
```
-Swift:
+#### Swift:
```swift
func minDepth(_ root: TreeNode?) -> Int {
@@ -3082,7 +3097,7 @@ func minDepth(_ root: TreeNode?) -> Int {
}
```
-Scala:
+#### Scala:
```scala
// 111.二叉树的最小深度
@@ -3108,7 +3123,7 @@ object Solution {
}
```
-rust:
+#### Rust:
```rust
use std::cell::RefCell;
@@ -3141,28 +3156,27 @@ impl Solution {
}
```
-# 总结
+## 总结
二叉树的层序遍历,**就是图论中的广度优先搜索在二叉树中的应用**,需要借助队列来实现(此时又发现队列的一个应用了)。
来吧,一口气打十个:
-* 102.二叉树的层序遍历
-* 107.二叉树的层次遍历II
-* 199.二叉树的右视图
-* 637.二叉树的层平均值
-* 429.N叉树的层序遍历
-* 515.在每个树行中找最大值
-* 116.填充每个节点的下一个右侧节点指针
-* 117.填充每个节点的下一个右侧节点指针II
-* 104.二叉树的最大深度
-* 111.二叉树的最小深度
+* [102.二叉树的层序遍历](https://leetcode.cn/problems/binary-tree-level-order-traversal/)
+* [107.二叉树的层次遍历II](https://leetcode.cn/problems/binary-tree-level-order-traversal-ii/)
+* [199.二叉树的右视图](https://leetcode.cn/problems/binary-tree-right-side-view/)
+* [637.二叉树的层平均值](https://leetcode.cn/problems/binary-tree-right-side-view/)
+* [429.N叉树的层序遍历](https://leetcode.cn/problems/n-ary-tree-level-order-traversal/)
+* [515.在每个树行中找最大值](https://leetcode.cn/problems/find-largest-value-in-each-tree-row/)
+* [116.填充每个节点的下一个右侧节点指针](https://leetcode.cn/problems/populating-next-right-pointers-in-each-node/)
+* [117.填充每个节点的下一个右侧节点指针II](https://leetcode.cn/problems/populating-next-right-pointers-in-each-node-ii/)
+* [104.二叉树的最大深度](https://leetcode.cn/problems/maximum-depth-of-binary-tree/)
+* [111.二叉树的最小深度](https://leetcode.cn/problems/minimum-depth-of-binary-tree/)
**致敬叶师傅!**
-
-
+
diff --git a/problems/0104.二叉树的最大深度.md b/problems/0104.二叉树的最大深度.md
index 7130867b..05044375 100644
--- a/problems/0104.二叉树的最大深度.md
+++ b/problems/0104.二叉树的最大深度.md
@@ -5,6 +5,7 @@
参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!
+
# 104.二叉树的最大深度
[力扣题目链接](https://leetcode.cn/problems/maximum-depth-of-binary-tree/)
@@ -23,17 +24,19 @@
返回它的最大深度 3 。
-# 思路
+## 算法公开课
+
+**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[二叉树的高度和深度有啥区别?究竟用什么遍历顺序?很多录友搞不懂 | 104.二叉树的最大深度](https://www.bilibili.com/video/BV1Gd4y1V75u),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
+
+## 思路
看完本篇可以一起做了如下两道题目:
-* 104.二叉树的最大深度
-* 559.n叉树的最大深度
-
-《代码随想录》算法视频公开课:[二叉树的高度和深度有啥区别?究竟用什么遍历顺序?很多录友搞不懂 | 104.二叉树的最大深度](https://www.bilibili.com/video/BV1Gd4y1V75u),相信结合视频在看本篇题解,更有助于大家对本题的理解。
+* [104.二叉树的最大深度](https://leetcode.cn/problems/maximum-depth-of-binary-tree/)
+* [559.n叉树的最大深度](https://leetcode.cn/problems/maximum-depth-of-n-ary-tree/)
-## 递归法
+### 递归法
本题可以使用前序(中左右),也可以使用后序遍历(左右中),使用前序求的就是深度,使用后序求的是高度。
@@ -164,7 +167,7 @@ public:
};
```
-## 迭代法
+### 迭代法
使用迭代法的话,使用层序遍历是最为合适的,因为最大的深度就是二叉树的层数,和层序遍历的方式极其吻合。
@@ -202,10 +205,11 @@ public:
};
```
-
那么我们可以顺便解决一下n叉树的最大深度问题
-# 559.n叉树的最大深度
+## 相关题目推荐
+
+### 559.n叉树的最大深度
[力扣题目链接](https://leetcode.cn/problems/maximum-depth-of-n-ary-tree/)
@@ -219,11 +223,11 @@ public:
我们应返回其最大深度,3。
-思路:
+### 思路
依然可以提供递归法和迭代法,来解决这个问题,思路是和二叉树思路一样的,直接给出代码如下:
-## 递归法
+#### 递归法
c++代码:
@@ -240,7 +244,7 @@ public:
}
};
```
-## 迭代法
+#### 迭代法
依然是层序遍历,代码如下:
@@ -267,11 +271,11 @@ public:
};
```
-# 其他语言版本
+## 其他语言版本
-## java
+### Java:
-### 104.二叉树的最大深度
+104.二叉树的最大深度
```java
class solution {
@@ -344,7 +348,8 @@ class solution {
}
```
-### 559.n叉树的最大深度
+559.n叉树的最大深度
+
```java
class Solution {
/*递归法,后序遍历求root节点的高度*/
@@ -391,9 +396,9 @@ class solution {
}
```
-## python
+### Python :
-### 104.二叉树的最大深度
+104.二叉树的最大深度
递归法:
```python
@@ -448,7 +453,7 @@ class Solution:
```
-### 559.n叉树的最大深度
+559.n叉树的最大深度
递归法:
```python
@@ -522,9 +527,10 @@ class Solution:
return max_depth
```
+### Go:
+
+104.二叉树的最大深度
-## go
-### 104.二叉树的最大深度
```go
/**
* definition for a binary tree node.
@@ -574,7 +580,7 @@ func maxdepth(root *treenode) int {
```
-### 559. n叉树的最大深度
+559. n叉树的最大深度
```go
func maxDepth(root *Node) int {
@@ -598,9 +604,9 @@ func maxDepth(root *Node) int {
}
```
-## javascript
+### Javascript :
-### 104.二叉树的最大深度
+104.二叉树的最大深度
```javascript
var maxdepth = function(root) {
@@ -649,7 +655,7 @@ var maxDepth = function(root) {
};
```
-### 559.n叉树的最大深度
+559.n叉树的最大深度
N叉树的最大深度 递归写法
```js
@@ -683,9 +689,9 @@ var maxDepth = function(root) {
};
```
-## TypeScript
+### TypeScript:
-### 104.二叉树的最大深度
+104.二叉树的最大深度
```typescript
// 后续遍历(自下而上)
@@ -728,7 +734,7 @@ function maxDepth(root: TreeNode | null): number {
};
```
-### 559.n叉树的最大深度
+559.n叉树的最大深度
```typescript
// 后续遍历(自下而上)
@@ -756,9 +762,9 @@ function maxDepth(root: TreeNode | null): number {
```
-## C
+### C:
-### 104.二叉树的最大深度
+104.二叉树的最大深度
二叉树最大深度递归
```c
@@ -814,9 +820,9 @@ int maxDepth(struct TreeNode* root){
}
```
-## Swift
+### Swift:
-### 104.二叉树的最大深度
+104.二叉树的最大深度
```swift
// 递归 - 后序
@@ -856,7 +862,7 @@ func maxDepth(_ root: TreeNode?) -> Int {
}
```
-### 559.n叉树的最大深度
+559.n叉树的最大深度
```swift
// 递归
@@ -893,9 +899,10 @@ func maxDepth1(_ root: Node?) -> Int {
}
```
-## Scala
+### Scala:
+
+104.二叉树的最大深度
-### 104.二叉树的最大深度
递归法:
```scala
object Solution {
@@ -934,7 +941,7 @@ object Solution {
}
```
-### 559.n叉树的最大深度
+559.n叉树的最大深度
递归法:
```scala
@@ -972,8 +979,8 @@ object Solution {
}
```
-## rust
-### 0104.二叉树的最大深度
+### Rust:
+0104.二叉树的最大深度
递归:
```rust
diff --git a/problems/0106.从中序与后序遍历序列构造二叉树.md b/problems/0106.从中序与后序遍历序列构造二叉树.md
index a0bab999..0144fc5c 100644
--- a/problems/0106.从中序与后序遍历序列构造二叉树.md
+++ b/problems/0106.从中序与后序遍历序列构造二叉树.md
@@ -29,9 +29,9 @@

-# 视频讲解
+## 算法公开课
-**《代码随想录》算法视频公开课:[坑很多!来看看你掉过几次坑 | LeetCode:106.从中序与后序遍历序列构造二叉树](https://www.bilibili.com/video/BV1vW4y1i7dn),相信结合视频在看本篇题解,更有助于大家对本题的理解**。
+**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[坑很多!来看看你掉过几次坑 | LeetCode:106.从中序与后序遍历序列构造二叉树](https://www.bilibili.com/video/BV1vW4y1i7dn),相信结合视频在看本篇题解,更有助于大家对本题的理解**。
## 思路
@@ -158,8 +158,6 @@ root->right = traversal(rightInorder, rightPostorder);
完整代码如下:
-### C++完整代码
-
```CPP
class Solution {
private:
@@ -281,8 +279,6 @@ public:
下面给出用下标索引写出的代码版本:(思路是一样的,只不过不用重复定义vector了,每次用下标索引来分割)
-### C++优化版本
-
```CPP
class Solution {
private:
@@ -400,8 +396,9 @@ public:
};
```
+## 相关题目推荐
-# 105.从前序与中序遍历序列构造二叉树
+### 105.从前序与中序遍历序列构造二叉树
[力扣题目链接](https://leetcode.cn/problems/construct-binary-tree-from-preorder-and-inorder-traversal/)
@@ -418,7 +415,7 @@ public:

-## 思路
+### 思路
本题和106是一样的道理。
@@ -547,7 +544,7 @@ public:
};
```
-# 思考题
+## 思考题
前序和中序可以唯一确定一棵二叉树。
@@ -569,7 +566,7 @@ tree2 的前序遍历是[1 2 3], 后序遍历是[3 2 1]。
所以前序和后序不能唯一确定一棵二叉树!
-# 总结
+## 总结
之前我们讲的二叉树题目都是各种遍历二叉树,这次开始构造二叉树了,思路其实比较简单,但是真正代码实现出来并不容易。
@@ -585,9 +582,9 @@ tree2 的前序遍历是[1 2 3], 后序遍历是[3 2 1]。
-# 其他语言版本
+## 其他语言版本
-## Java
+### Java
106.从中序与后序遍历序列构造二叉树
@@ -688,7 +685,7 @@ class Solution {
}
```
-## Python
+### Python
105.从前序与中序遍历序列构造二叉树
@@ -754,7 +751,7 @@ class Solution:
return root
```
-## Go
+### Go
106 从中序与后序遍历序列构造二叉树
@@ -833,9 +830,7 @@ func build(pre []int, in []int, root int, l, r int) *TreeNode {
```
-
-
-## JavaScript
+### JavaScript
```javascript
var buildTree = function(inorder, postorder) {
@@ -863,7 +858,7 @@ var buildTree = function(preorder, inorder) {
};
```
-## TypeScript
+### TypeScript
> 106.从中序与后序遍历序列构造二叉树
@@ -969,7 +964,7 @@ function buildTree(preorder: number[], inorder: number[]): TreeNode | null {
};
```
-## C
+### C
106 从中序与后序遍历序列构造二叉树
@@ -1047,7 +1042,7 @@ struct TreeNode* buildTree(int* preorder, int preorderSize, int* inorder, int in
}
```
-## Swift
+### Swift
105 从前序与中序遍历序列构造二叉树
@@ -1140,7 +1135,7 @@ class Solution_0106 {
}
```
-## Scala
+### Scala
106 从中序与后序遍历序列构造二叉树
@@ -1188,7 +1183,7 @@ object Solution {
}
```
-## rust
+### Rust
106 从中序与后序遍历序列构造二叉树
@@ -1238,3 +1233,4 @@ impl Solution {
+
diff --git a/problems/0108.将有序数组转换为二叉搜索树.md b/problems/0108.将有序数组转换为二叉搜索树.md
index 056ef3e2..e699005e 100644
--- a/problems/0108.将有序数组转换为二叉搜索树.md
+++ b/problems/0108.将有序数组转换为二叉搜索树.md
@@ -20,11 +20,11 @@

-# 算法公开课
+## 算法公开课
-**《代码随想录》算法视频公开课:[构造平衡二叉搜索树!| LeetCode:108.将有序数组转换为二叉搜索树](https://www.bilibili.com/video/BV1uR4y1X7qL?share_source=copy_web),相信结合视频在看本篇题解,更有助于大家对本题的理解**。
+**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[构造平衡二叉搜索树!| LeetCode:108.将有序数组转换为二叉搜索树](https://www.bilibili.com/video/BV1uR4y1X7qL?share_source=copy_web),相信结合视频在看本篇题解,更有助于大家对本题的理解**。
-# 思路
+## 思路
做这道题目之前大家可以了解一下这几道:
@@ -71,7 +71,7 @@
**这也是题目中强调答案不是唯一的原因。 理解这一点,这道题目算是理解到位了**。
-## 递归
+### 递归
递归三部曲:
@@ -155,7 +155,7 @@ public:
**注意:在调用traversal的时候传入的left和right为什么是0和nums.size() - 1,因为定义的区间为左闭右闭**。
-## 迭代法
+### 迭代法
迭代法可以通过三个队列来模拟,一个队列放遍历的节点,一个队列放左区间下标,一个队列放右区间下标。
@@ -203,7 +203,7 @@ public:
};
```
-# 总结
+## 总结
**在[二叉树:构造二叉树登场!](https://programmercarl.com/0106.从中序与后序遍历序列构造二叉树.html) 和 [二叉树:构造一棵最大的二叉树](https://programmercarl.com/0654.最大二叉树.html)之后,我们顺理成章的应该构造一下二叉搜索树了,一不小心还是一棵平衡二叉搜索树**。
@@ -216,10 +216,10 @@ public:
最后依然给出迭代的方法,其实就是模拟取中间元素,然后不断分割去构造二叉树的过程。
-# 其他语言版本
+## 其他语言版本
-## Java
+### Java
递归: 左闭右开 [left,right)
```Java
@@ -315,7 +315,7 @@ class Solution {
}
```
-## Python
+### Python
递归法
```python
class Solution:
@@ -377,7 +377,7 @@ class Solution:
```
-## Go
+### Go
递归(隐含回溯)
@@ -396,7 +396,7 @@ func sortedArrayToBST(nums []int) *TreeNode {
}
```
-## JavaScript
+### JavaScript
递归
```javascript
@@ -453,7 +453,7 @@ var sortedArrayToBST = function(nums) {
return root;
};
```
-## TypeScript
+### TypeScript
```typescript
function sortedArrayToBST(nums: number[]): TreeNode | null {
@@ -469,7 +469,7 @@ function sortedArrayToBST(nums: number[]): TreeNode | null {
};
```
-## C
+### C
递归
```c
@@ -490,7 +490,7 @@ struct TreeNode* sortedArrayToBST(int* nums, int numsSize) {
}
```
-## Scala
+### Scala
递归:
@@ -511,7 +511,7 @@ object Solution {
}
```
-## rust
+### Rust
递归:
@@ -536,3 +536,4 @@ impl Solution {
+
diff --git a/problems/0110.平衡二叉树.md b/problems/0110.平衡二叉树.md
index e10a612a..c7df9c8f 100644
--- a/problems/0110.平衡二叉树.md
+++ b/problems/0110.平衡二叉树.md
@@ -33,8 +33,9 @@
返回 false 。
+## 算法公开课
-**《代码随想录》算法视频公开课:[后序遍历求高度,高度判断是否平衡 | LeetCode:110.平衡二叉树](https://www.bilibili.com/video/BV1Ug411S7my),相信结合视频在看本篇题解,更有助于大家对本题的理解**。
+**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[后序遍历求高度,高度判断是否平衡 | LeetCode:110.平衡二叉树](https://www.bilibili.com/video/BV1Ug411S7my),相信结合视频在看本篇题解,更有助于大家对本题的理解**。
## 题外话
@@ -357,7 +358,7 @@ public:
## 其他语言版本
-### Java
+### Java:
```Java
class Solution {
@@ -498,7 +499,7 @@ class Solution {
}
```
-### Python
+### Python:
递归法:
@@ -620,7 +621,7 @@ class Solution:
height_map[real_node] = 1 + max(left, right)
return True
```
-### Go
+### Go:
```Go
func isBalanced(root *TreeNode) bool {
@@ -652,7 +653,7 @@ func max(a, b int) int {
}
```
-### JavaScript
+### JavaScript:
递归法:
@@ -723,7 +724,7 @@ var isBalanced = function (root) {
};
```
-### TypeScript
+### TypeScript:
```typescript
// 递归法
@@ -741,7 +742,7 @@ function isBalanced(root: TreeNode | null): boolean {
};
```
-### C
+### C:
递归法:
@@ -876,7 +877,7 @@ func getHeight(_ root: TreeNode?) -> Int {
}
```
-### rust
+### Rust:
递归
@@ -912,3 +913,4 @@ impl Solution {
+
diff --git a/problems/0111.二叉树的最小深度.md b/problems/0111.二叉树的最小深度.md
index a1fc8a93..61f9beb7 100644
--- a/problems/0111.二叉树的最小深度.md
+++ b/problems/0111.二叉树的最小深度.md
@@ -26,9 +26,11 @@
返回它的最小深度 2.
-# 思路
+## 算法公开课
-《代码随想录》算法视频公开课:[看起来好像做过,一写就错! | LeetCode:111.二叉树的最小深度](https://www.bilibili.com/video/BV1QD4y1B7e2),相信结合视频在看本篇题解,更有助于大家对本题的理解。
+**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[看起来好像做过,一写就错! | LeetCode:111.二叉树的最小深度](https://www.bilibili.com/video/BV1QD4y1B7e2),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
+
+## 思路
看完了这篇[104.二叉树的最大深度](https://programmercarl.com/0104.二叉树的最大深度.html),再来看看如何求最小深度。
@@ -52,7 +54,7 @@
什么是叶子节点,左右孩子都为空的节点才是叶子节点!
-## 递归法
+### 递归法
来来来,一起递归三部曲:
@@ -199,7 +201,7 @@ public:
};
```
-## 迭代法
+### 迭代法
相对于[104.二叉树的最大深度](https://programmercarl.com/0104.二叉树的最大深度.html),本题还可以使用层序遍历的方式来解决,思路是一样的。
@@ -237,10 +239,10 @@ public:
```
-# 其他语言版本
+## 其他语言版本
-## Java
+### Java:
```Java
class Solution {
@@ -300,7 +302,7 @@ class Solution {
}
```
-## Python
+### Python :
递归法(版本一)
@@ -400,9 +402,7 @@ class Solution:
return depth
```
-
-
-## Go
+### Go:
```go
/**
@@ -463,7 +463,7 @@ func minDepth(root *TreeNode) int {
```
-## JavaScript
+### JavaScript:
递归法:
@@ -509,7 +509,7 @@ var minDepth = function(root) {
};
```
-## TypeScript
+### TypeScript:
> 递归法
@@ -547,7 +547,7 @@ function minDepth(root: TreeNode | null): number {
};
```
-## Swift
+### Swift:
> 递归
```Swift
@@ -594,7 +594,7 @@ func minDepth(_ root: TreeNode?) -> Int {
```
-## Scala
+### Scala:
递归法:
```scala
@@ -633,7 +633,8 @@ object Solution {
}
```
-rust:
+### Rust:
+
```rust
impl Solution {
// 递归
diff --git a/problems/0112.路径总和.md b/problems/0112.路径总和.md
index 39285a3b..be03f719 100644
--- a/problems/0112.路径总和.md
+++ b/problems/0112.路径总和.md
@@ -21,9 +21,9 @@
返回 true, 因为存在目标和为 22 的根节点到叶子节点的路径 5->4->11->2。
-## 视频讲解
+## 算法公开课
-**《代码随想录》算法视频公开课:[拿不准的遍历顺序,搞不清的回溯过程,我太难了! | LeetCode:112. 路径总和](https://www.bilibili.com/video/BV19t4y1L7CR),相信结合视频在看本篇题解,更有助于大家对本题的理解**。
+**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[拿不准的遍历顺序,搞不清的回溯过程,我太难了! | LeetCode:112. 路径总和](https://www.bilibili.com/video/BV19t4y1L7CR),相信结合视频在看本篇题解,更有助于大家对本题的理解**。
## 思路
@@ -32,8 +32,8 @@
那么接下来我通过详细讲解如下两道题,来回答这个问题:
-* 112.路径总和
-* 113.路径总和ii
+* [112.路径总和](https://leetcode.cn/problems/path-sum/)
+* [113.路径总和ii](https://leetcode.cn/problems/path-sum-ii/)
这道题我们要遍历从根节点到叶子节点的路径看看总和是不是目标和。
@@ -218,7 +218,9 @@ public:
如果大家完全理解了本题的递归方法之后,就可以顺便把leetcode上113. 路径总和ii做了。
-# 113. 路径总和ii
+## 相关题目推荐
+
+### 113. 路径总和ii
[力扣题目链接](https://leetcode.cn/problems/path-sum-ii/)
@@ -232,7 +234,7 @@ public:

-## 思路
+### 思路
113.路径总和ii要遍历整个树,找到所有路径,**所以递归函数不要返回值!**
@@ -289,7 +291,7 @@ public:
至于113. 路径总和ii 的迭代法我并没有写,用迭代方式记录所有路径比较麻烦,也没有必要,如果大家感兴趣的话,可以再深入研究研究。
-## 总结
+### 总结
本篇通过leetcode上112. 路径总和 和 113. 路径总和ii 详细的讲解了 递归函数什么时候需要返回值,什么不需要返回值。
@@ -300,11 +302,11 @@ public:
-# 其他语言版本
+## 其他语言版本
-## java
+### Java
-### 0112.路径总和
+0112.路径总和
```java
class solution {
@@ -422,7 +424,7 @@ class solution {
}
```
-### 0113.路径总和-ii
+0113.路径总和-ii
```java
class solution {
@@ -529,9 +531,9 @@ class Solution {
}
```
-## python
+### Python
-### 0112.路径总和
+0112.路径总和
(版本一) 递归
```python
@@ -618,7 +620,7 @@ class Solution:
-### 0113.路径总和-ii
+0113.路径总和-ii
(版本一) 递归
```python
@@ -719,9 +721,9 @@ class Solution:
```
-## go
+### Go
-### 112. 路径总和
+112. 路径总和
```go
//递归法
@@ -746,7 +748,7 @@ func hasPathSum(root *TreeNode, targetSum int) bool {
}
```
-### 113. 路径总和 II
+113. 路径总和 II
```go
/**
@@ -786,9 +788,9 @@ func traverse(node *TreeNode, result *[][]int, currPath *[]int, targetSum int) {
}
```
-## javascript
+### Javascript
-### 0112.路径总和
+0112.路径总和
**递归**
@@ -852,7 +854,7 @@ let hasPathSum = function(root, targetSum) {
};
```
-### 0113.路径总和-ii
+0113.路径总和-ii
**递归**
@@ -950,9 +952,9 @@ let pathSum = function(root, targetSum) {
};
```
-## TypeScript
+### TypeScript
-### 0112.路径总和
+0112.路径总和
**递归法:**
@@ -1034,7 +1036,7 @@ function hasPathSum(root: TreeNode | null, targetSum: number): boolean {
};
```
-### 0112.路径总和 ii
+0112.路径总和 ii
**递归法:**
@@ -1070,9 +1072,9 @@ function pathSum(root: TreeNode | null, targetSum: number): number[][] {
};
```
-## Swift
+### Swift
-### 0112.路径总和
+0112.路径总和
**递归**
@@ -1141,7 +1143,7 @@ func hasPathSum(_ root: TreeNode?, _ targetSum: Int) -> Bool {
}
```
-### 0113.路径总和 II
+0113.路径总和 II
**递归**
@@ -1192,10 +1194,11 @@ func traversal(_ cur: TreeNode?, count: Int) {
}
```
-## C
+### C
-> 0112.路径总和
-> 递归法:
+0112.路径总和
+
+递归法:
```c
bool hasPathSum(struct TreeNode* root, int targetSum){
@@ -1252,7 +1255,7 @@ bool hasPathSum(struct TreeNode* root, int targetSum){
}
```
-> 0113.路径总和 II
+0113.路径总和 II
```c
int** ret;
@@ -1317,9 +1320,9 @@ int** pathSum(struct TreeNode* root, int targetSum, int* returnSize, int** retur
}
```
-## Scala
+### Scala
-### 0112.路径总和
+0112.路径总和
**递归:**
@@ -1369,7 +1372,7 @@ object Solution {
}
```
-### 0113.路径总和 II
+0113.路径总和 II
**递归:**
@@ -1405,9 +1408,9 @@ object Solution {
}
```
-## rust
+### Rust
-### 112.路径总和.md
+0112.路径总和
递归:
@@ -1461,7 +1464,7 @@ impl Solution {
}
```
-### 113.路径总和-ii
+0113.路径总和-ii
```rust
impl Solution {
diff --git a/problems/0115.不同的子序列.md b/problems/0115.不同的子序列.md
index 8c82880d..d925c5de 100644
--- a/problems/0115.不同的子序列.md
+++ b/problems/0115.不同的子序列.md
@@ -157,8 +157,8 @@ public:
## 其他语言版本
+### Java:
-Java:
```java
class Solution {
public int numDistinct(String s, String t) {
@@ -182,7 +182,8 @@ class Solution {
}
```
-Python:
+### Python:
+
```python
class Solution:
def numDistinct(self, s: str, t: str) -> int:
@@ -200,7 +201,8 @@ class Solution:
return dp[-1][-1]
```
-Python3:
+### Python3:
+
```python
class SolutionDP2:
"""
@@ -234,7 +236,8 @@ class SolutionDP2:
return dp[-1]
```
-Go:
+### Go:
+
```go
func numDistinct(s string, t string) int {
dp:= make([][]int,len(s)+1)
@@ -259,8 +262,8 @@ func numDistinct(s string, t string) int {
}
```
+### Javascript:
-Javascript:
```javascript
const numDistinct = (s, t) => {
let dp = Array.from(Array(s.length + 1), () => Array(t.length +1).fill(0));
@@ -283,7 +286,7 @@ const numDistinct = (s, t) => {
};
```
-TypeScript:
+### TypeScript:
```typescript
function numDistinct(s: string, t: string): number {
@@ -312,9 +315,8 @@ function numDistinct(s: string, t: string): number {
```
-
-
+
diff --git a/problems/0116.填充每个节点的下一个右侧节点指针.md b/problems/0116.填充每个节点的下一个右侧节点指针.md
index 31bb6822..003ef75a 100644
--- a/problems/0116.填充每个节点的下一个右侧节点指针.md
+++ b/problems/0116.填充每个节点的下一个右侧节点指针.md
@@ -30,7 +30,7 @@ struct Node {

-# 思路
+## 思路
注意题目提示内容,:
* 你只能使用常量级额外空间。
@@ -38,7 +38,7 @@ struct Node {
基本上就是要求使用递归了,迭代的方式一定会用到栈或者队列。
-## 递归
+### 递归
一想用递归怎么做呢,虽然层序遍历是最直观的,但是递归的方式确实不好想。
@@ -83,7 +83,7 @@ public:
};
```
-## 迭代(层序遍历)
+### 迭代(层序遍历)
本题使用层序遍历是最为直观的,如果对层序遍历不了解,看这篇:[二叉树:层序遍历登场!](https://programmercarl.com/0102.二叉树的层序遍历.html)。
@@ -114,9 +114,9 @@ public:
};
```
-# 其他语言版本
+## 其他语言版本
-## Java
+### Java
```java
// 递归法
@@ -169,7 +169,7 @@ class Solution {
}
```
-## Python
+### Python
```python
# 递归法
@@ -210,7 +210,7 @@ class Solution:
nodePre.next = None # 本层最后一个节点指向None
return root
```
-## Go
+### Go
```go
// 迭代法
func connect(root *Node) *Node {
@@ -259,7 +259,7 @@ func connect(root *Node) *Node {
}
```
-## JavaScript
+### JavaScript
```js
const connect = root => {
@@ -287,7 +287,7 @@ const connect = root => {
};
```
-## TypeScript
+### TypeScript
(注:命名空间‘Node’与typescript中内置类型冲突,这里改成了‘NodePro’)
@@ -365,3 +365,4 @@ function connect(root: NodePro | null): NodePro | null {
+
diff --git a/problems/0121.买卖股票的最佳时机.md b/problems/0121.买卖股票的最佳时机.md
index 06305156..cbdf40e8 100644
--- a/problems/0121.买卖股票的最佳时机.md
+++ b/problems/0121.买卖股票的最佳时机.md
@@ -24,11 +24,9 @@
* 输出:0
解释:在这种情况下, 没有交易完成, 所以最大利润为 0。
-# 算法公开课
-
-**《代码随想录》算法视频公开课:[动态规划之 LeetCode:121.买卖股票的最佳时机1](https://www.bilibili.com/video/BV1Xe4y1u77q),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
-
+## 算法公开课
+**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[动态规划之 LeetCode:121.买卖股票的最佳时机1](https://www.bilibili.com/video/BV1Xe4y1u77q),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
## 思路
@@ -202,7 +200,7 @@ public:
## 其他语言版本
-Java:
+### Java:
> 贪心法:
@@ -294,8 +292,7 @@ class Solution {
```
-
-Python:
+### Python:
> 贪心法:
```python
@@ -351,7 +348,8 @@ class Solution:
return dp1
```
-Go:
+### Go:
+
> 贪心法:
```Go
func maxProfit(prices []int) int {
@@ -418,7 +416,7 @@ func max(a, b int) int {
}
```
-JavaScript:
+### JavaScript:
> 动态规划
@@ -454,7 +452,7 @@ var maxProfit = function(prices) {
};
```
-TypeScript:
+### TypeScript:
> 贪心法
@@ -492,7 +490,7 @@ function maxProfit(prices: number[]): number {
};
```
-C#:
+### C#:
> 贪心法
@@ -533,7 +531,7 @@ public class Solution
}
```
-Rust:
+### Rust:
> 贪心
diff --git a/problems/0122.买卖股票的最佳时机II.md b/problems/0122.买卖股票的最佳时机II.md
index 89c654fa..2c2ab225 100644
--- a/problems/0122.买卖股票的最佳时机II.md
+++ b/problems/0122.买卖股票的最佳时机II.md
@@ -37,9 +37,9 @@
- 1 <= prices.length <= 3 \* 10 ^ 4
- 0 <= prices[i] <= 10 ^ 4
-# 视频讲解
+## 算法公开课
-**《代码随想录》算法视频公开课:[贪心算法也能解决股票问题!LeetCode:122.买卖股票最佳时机 II](https://www.bilibili.com/video/BV1ev4y1C7na),相信结合视频在看本篇题解,更有助于大家对本题的理解**。
+**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[贪心算法也能解决股票问题!LeetCode:122.买卖股票最佳时机 II](https://www.bilibili.com/video/BV1ev4y1C7na),相信结合视频在看本篇题解,更有助于大家对本题的理解**。
## 思路
@@ -316,7 +316,7 @@ function maxProfit(prices: number[]): number {
}
```
-### Rust
+### Rust:
贪心:
@@ -389,7 +389,7 @@ int maxProfit(int* prices, int pricesSize){
}
```
-### Scala
+### Scala:
贪心:
@@ -411,3 +411,4 @@ object Solution {
+
diff --git a/problems/0122.买卖股票的最佳时机II(动态规划).md b/problems/0122.买卖股票的最佳时机II(动态规划).md
index 02f8d287..6e08b57c 100644
--- a/problems/0122.买卖股票的最佳时机II(动态规划).md
+++ b/problems/0122.买卖股票的最佳时机II(动态规划).md
@@ -34,9 +34,9 @@
* 1 <= prices.length <= 3 * 10 ^ 4
* 0 <= prices[i] <= 10 ^ 4
-# 算法公开课
+## 算法公开课
-**《代码随想录》算法视频公开课:[动态规划,股票问题第二弹 | LeetCode:122.买卖股票的最佳时机II](https://www.bilibili.com/video/BV1D24y1Q7Ls),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
+**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[动态规划,股票问题第二弹 | LeetCode:122.买卖股票的最佳时机II](https://www.bilibili.com/video/BV1D24y1Q7Ls),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
## 思路
@@ -133,8 +133,8 @@ public:
## 其他语言版本
+### Java:
-Java:
```java
// 动态规划
class Solution
@@ -191,7 +191,7 @@ class Solution {
}
```
-Python:
+### Python:
> 版本一:
```python
@@ -221,7 +221,8 @@ class Solution:
return dp[(length-1) % 2][1]
```
-Go:
+### Go:
+
```go
// 买卖股票的最佳时机Ⅱ 动态规划
// 时间复杂度:O(n) 空间复杂度:O(n)
@@ -250,7 +251,8 @@ func max(a, b int) int {
}
```
-Javascript:
+### JavaScript:
+
```javascript
// 方法一:动态规划(dp 数组)
const maxProfit = (prices) => {
@@ -290,7 +292,7 @@ const maxProfit = (prices) => {
}
```
-TypeScript:
+### TypeScript:
> 动态规划
@@ -326,7 +328,7 @@ function maxProfit(prices: number[]): number {
};
```
-C#:
+### C#:
> 贪心法
@@ -363,7 +365,7 @@ public class Solution
}
```
-Rust:
+### Rust:
> 贪心
@@ -414,4 +416,3 @@ impl Solution {
-
diff --git a/problems/0123.买卖股票的最佳时机III.md b/problems/0123.买卖股票的最佳时机III.md
index a646b7d5..72dd9042 100644
--- a/problems/0123.买卖股票的最佳时机III.md
+++ b/problems/0123.买卖股票的最佳时机III.md
@@ -39,9 +39,9 @@
* 1 <= prices.length <= 10^5
* 0 <= prices[i] <= 10^5
-# 算法公开课
+## 算法公开课
-**《代码随想录》算法视频公开课:[动态规划,股票至多买卖两次,怎么求? | LeetCode:123.买卖股票最佳时机III](https://www.bilibili.com/video/BV1WG411K7AR),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
+**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[动态规划,股票至多买卖两次,怎么求? | LeetCode:123.买卖股票最佳时机III](https://www.bilibili.com/video/BV1WG411K7AR),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
## 思路
@@ -221,7 +221,7 @@ public:
## 其他语言版本
-Java:
+### Java:
```java
// 版本一
@@ -277,7 +277,7 @@ class Solution {
}
```
-Python:
+### Python:
> 版本一:
```python
@@ -314,7 +314,7 @@ class Solution:
return dp[4]
```
-Go:
+### Go:
```go
func maxProfit(prices []int) int {
@@ -344,7 +344,7 @@ func max(a, b int) int {
}
```
-JavaScript:
+### JavaScript:
> 版本一:
@@ -383,7 +383,7 @@ const maxProfit = prices => {
};
```
-TypeScript:
+### TypeScript:
> 版本一
@@ -413,7 +413,7 @@ function maxProfit(prices: number[]): number {
};
```
-Rust:
+### Rust:
> 版本一
@@ -465,3 +465,4 @@ impl Solution {
+
diff --git a/problems/0127.单词接龙.md b/problems/0127.单词接龙.md
index 20ad5182..97bc66d0 100644
--- a/problems/0127.单词接龙.md
+++ b/problems/0127.单词接龙.md
@@ -29,7 +29,7 @@
* 解释:endWord "cog" 不在字典中,所以无法进行转换。
-# 思路
+## 思路
以示例1为例,从这个图中可以看出 hit 到 cog的路线,不止一条,有三条,一条是最短的长度为5,两条长度为6。
@@ -97,9 +97,9 @@ public:
当然本题也可以用双向BFS,就是从头尾两端进行搜索,大家感兴趣,可以自己去实现,这里就不再做详细讲解了。
-# 其他语言版本
+## 其他语言版本
-## Java
+### Java
```java
public int ladderLength(String beginWord, String endWord, List wordList) {
@@ -196,7 +196,7 @@ class Solution {
}
```
-## Python
+### Python
```
class Solution:
@@ -221,7 +221,7 @@ class Solution:
queue.append(newWord)
return 0
```
-## Go
+### Go
```go
func ladderLength(beginWord string, endWord string, wordList []string) int {
wordMap, que, depth := getWordMap(wordList, beginWord), []string{beginWord}, 0
@@ -274,7 +274,7 @@ func getCandidates(word string) []string {
}
```
-## JavaScript
+### JavaScript
```javascript
var ladderLength = function(beginWord, endWord, wordList) {
// 将wordList转成Set,提高查询速度
@@ -310,7 +310,7 @@ var ladderLength = function(beginWord, endWord, wordList) {
};
```
-## TypeScript
+### TypeScript
```typescript
function ladderLength(
beginWord: string,
@@ -364,4 +364,3 @@ function diffonechar(word1: string, word2: string): boolean {
-
diff --git a/problems/0129.求根到叶子节点数字之和.md b/problems/0129.求根到叶子节点数字之和.md
index 445e108a..ebb36071 100644
--- a/problems/0129.求根到叶子节点数字之和.md
+++ b/problems/0129.求根到叶子节点数字之和.md
@@ -10,7 +10,7 @@
[力扣题目链接](https://leetcode.cn/problems/sum-root-to-leaf-numbers/)
-# 思路
+## 思路
本题和[113.路径总和II](https://programmercarl.com/0112.路径总和.html#_113-路径总和ii)是类似的思路,做完这道题,可以顺便把[113.路径总和II](https://programmercarl.com/0112.路径总和.html#_113-路径总和ii) 和 [112.路径总和](https://programmercarl.com/0112.路径总和.html#_112-路径总和) 做了。
@@ -24,7 +24,7 @@
那么先按递归三部曲来分析:
-## 递归三部曲
+### 递归三部曲
如果对递归三部曲不了解的话,可以看这里:[二叉树:前中后递归详解](https://programmercarl.com/二叉树的递归遍历.html)
@@ -116,7 +116,7 @@ path.pop_back(); // 回溯
```
**把回溯放在花括号外面了,世界上最遥远的距离,是你在花括号里,而我在花括号外!** 这就不对了。
-## 整体C++代码
+整体C++代码
关键逻辑分析完了,整体C++代码如下:
@@ -162,16 +162,16 @@ public:
};
```
-# 总结
+## 总结
过于简洁的代码,很容易让初学者忽视了本题中回溯的精髓,甚至作者本身都没有想清楚自己用了回溯。
**我这里提供的代码把整个回溯过程充分体现出来,希望可以帮助大家看的明明白白!**
-# 其他语言版本
+## 其他语言版本
-Java:
+### Java:
```java
class Solution {
@@ -219,7 +219,8 @@ class Solution {
}
```
-Python:
+### Python:
+
```python
class Solution:
def sumNumbers(self, root: TreeNode) -> int:
@@ -246,7 +247,7 @@ class Solution:
backtrace(root)
return res
```
-Go:
+### Go:
```go
func sumNumbers(root *TreeNode) int {
@@ -271,7 +272,8 @@ func dfs(root *TreeNode, tmpSum int, sum *int) {
-JavaScript:
+### JavaScript:
+
```javascript
var sumNumbers = function(root) {
const listToInt = path => {
@@ -315,7 +317,7 @@ var sumNumbers = function(root) {
};
```
-TypeScript:
+### TypeScript:
```typescript
function sumNumbers(root: TreeNode | null): number {
@@ -351,7 +353,7 @@ function sumNumbers(root: TreeNode | null): number {
};
```
-C:
+### C:
```c
//sum记录总和
@@ -384,3 +386,4 @@ int sumNumbers(struct TreeNode* root){
+
diff --git a/problems/0131.分割回文串.md b/problems/0131.分割回文串.md
index 92fed58a..ca73e9f7 100644
--- a/problems/0131.分割回文串.md
+++ b/problems/0131.分割回文串.md
@@ -23,12 +23,12 @@
["a","a","b"]
]
-# 算法公开课
+## 算法公开课
-**《代码随想录》算法视频公开课:[131.分割回文串](https://www.bilibili.com/video/BV1c54y1e7k6),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
+**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[131.分割回文串](https://www.bilibili.com/video/BV1c54y1e7k6),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
-# 思路
+## 思路
本题这涉及到两个关键问题:
@@ -58,7 +58,7 @@
此时可以发现,切割问题的回溯搜索的过程和组合问题的回溯搜索的过程是差不多的。
-## 回溯三部曲
+### 回溯三部曲
* 递归函数参数
@@ -124,7 +124,7 @@ for (int i = startIndex; i < s.size(); i++) {
**注意切割过的位置,不能重复切割,所以,backtracking(s, i + 1); 传入下一层的起始位置为i + 1**。
-## 判断回文子串
+### 判断回文子串
最后我们看一下回文子串要如何判断了,判断一个字符串是否是回文。
@@ -147,8 +147,6 @@ for (int i = startIndex; i < s.size(); i++) {
此时关键代码已经讲解完毕,整体代码如下(详细注释了)
-## C++整体代码
-
根据Carl给出的回溯算法模板:
```CPP
@@ -212,7 +210,7 @@ public:
* 时间复杂度: O(n * 2^n)
* 空间复杂度: O(n^2)
-# 优化
+## 优化
上面的代码还存在一定的优化空间, 在于如何更高效的计算一个子字符串是否是回文字串。上述代码```isPalindrome```函数运用双指针的方法来判定对于一个字符串```s```, 给定起始下标和终止下标, 截取出的子字符串是否是回文字串。但是其中有一定的重复计算存在:
@@ -272,7 +270,7 @@ public:
```
-# 总结
+## 总结
这道题目在leetcode上是中等,但可以说是hard的题目了,但是代码其实就是按照模板的样子来的。
@@ -306,10 +304,10 @@ public:
-# 其他语言版本
+## 其他语言版本
-## Java
+### Java
```Java
class Solution {
List> lists = new ArrayList<>();
@@ -351,7 +349,7 @@ class Solution {
}
```
-## Python
+### Python
回溯 基本版
```python
class Solution:
@@ -473,7 +471,7 @@ class Solution:
return all(s[i] == s[len(s) - 1 - i] for i in range(len(s) // 2))
```
-## Go
+### Go
```go
var (
path []string // 放已经回文的子串
@@ -512,7 +510,7 @@ func isPalindrome(s string) bool {
}
```
-## javaScript
+### JavaScript
```js
/**
@@ -545,7 +543,7 @@ var partition = function(s) {
};
```
-## TypeScript
+### TypeScript
```typescript
function partition(s: string): string[][] {
@@ -582,7 +580,7 @@ function partition(s: string): string[][] {
};
```
-## C
+### C
```c
char** path;
@@ -679,7 +677,7 @@ char*** partition(char* s, int* returnSize, int** returnColumnSizes){
}
```
-## Swift
+### Swift
```swift
func partition(_ s: String) -> [[String]] {
@@ -719,7 +717,7 @@ func partition(_ s: String) -> [[String]] {
}
```
-## Rust
+### Rust
**回溯+函数判断回文串**
```Rust
@@ -808,7 +806,7 @@ impl Solution {
```
-## Scala
+### Scala
```scala
object Solution {
@@ -855,3 +853,4 @@ object Solution {
+
diff --git a/problems/0132.分割回文串II.md b/problems/0132.分割回文串II.md
index 9b164dfb..eb91a189 100644
--- a/problems/0132.分割回文串II.md
+++ b/problems/0132.分割回文串II.md
@@ -34,7 +34,7 @@
* 1 <= s.length <= 2000
* s 仅由小写英文字母组成
-# 思路
+## 思路
我们在讲解回溯法系列的时候,讲过了这道题目[回溯算法:131.分割回文串](https://programmercarl.com/0131.分割回文串.html)。
@@ -201,9 +201,9 @@ public:
```
-# 其他语言版本
+## 其他语言版本
-## Java
+### Java
```java
class Solution {
@@ -257,7 +257,7 @@ class Solution {
}
```
-## Python
+### Python
```python
class Solution:
@@ -286,7 +286,7 @@ class Solution:
return dp[-1]
```
-## Go
+### Go
```go
func minCut(s string) int {
@@ -330,7 +330,7 @@ func min(i, j int) int {
}
```
-## JavaScript
+### JavaScript
```js
var minCut = function(s) {
@@ -376,3 +376,4 @@ var minCut = function(s) {
+
diff --git a/problems/0134.加油站.md b/problems/0134.加油站.md
index ad9acfbc..2f9539e8 100644
--- a/problems/0134.加油站.md
+++ b/problems/0134.加油站.md
@@ -45,12 +45,14 @@
* 解释:
你不能从 0 号或 1 号加油站出发,因为没有足够的汽油可以让你行驶到下一个加油站。我们从 2 号加油站出发,可以获得 4 升汽油。 此时油箱有 = 0 + 4 = 4 升汽油。开往 0 号加油站,此时油箱有 4 - 3 + 2 = 3 升汽油。开往 1 号加油站,此时油箱有 3 - 3 + 3 = 3 升汽油。你无法返回 2 号加油站,因为返程需要消耗 4 升汽油,但是你的油箱只有 3 升汽油。因此,无论怎样,你都不可能绕环路行驶一周。
-# 视频讲解
+## 算法公开课
-**《代码随想录》算法视频公开课:[贪心算法,得这么加油才能跑完全程!LeetCode :134.加油站](https://www.bilibili.com/video/BV1jA411r7WX),相信结合视频在看本篇题解,更有助于大家对本题的理解**。
+**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[贪心算法,得这么加油才能跑完全程!LeetCode :134.加油站](https://www.bilibili.com/video/BV1jA411r7WX),相信结合视频在看本篇题解,更有助于大家对本题的理解**。
+
+## 思路
-## 暴力方法
+### 暴力方法
暴力的方法很明显就是O(n^2)的,遍历每一个加油站为起点的情况,模拟一圈。
@@ -85,7 +87,7 @@ public:
* 空间复杂度:O(1)
-## 贪心算法(方法一)
+### 贪心算法(方法一)
直接从全局进行贪心选择,情况如下:
@@ -134,7 +136,7 @@ public:
但不管怎么说,解法毕竟还是巧妙的,不用过于执着于其名字称呼。
-## 贪心算法(方法二)
+### 贪心算法(方法二)
可以换一个思路,首先如果总油量减去总消耗大于等于零那么一定可以跑完一圈,说明 各个站点的加油站 剩油量rest[i]相加一定是大于等于零的。
@@ -633,3 +635,4 @@ object Solution {
+
diff --git a/problems/0135.分发糖果.md b/problems/0135.分发糖果.md
index cf3ccc8e..d130bd68 100644
--- a/problems/0135.分发糖果.md
+++ b/problems/0135.分发糖果.md
@@ -28,9 +28,9 @@
* 输出: 4
* 解释: 你可以分别给这三个孩子分发 1、2、1 颗糖果。第三个孩子只得到 1 颗糖果,这已满足上述两个条件。
-# 视频讲解
+## 算法公开课
-**《代码随想录》算法视频公开课:[贪心算法,两者兼顾很容易顾此失彼!LeetCode:135.分发糖果](https://www.bilibili.com/video/BV1ev4y1r7wN),相信结合视频在看本篇题解,更有助于大家对本题的理解**。
+**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[贪心算法,两者兼顾很容易顾此失彼!LeetCode:135.分发糖果](https://www.bilibili.com/video/BV1ev4y1r7wN),相信结合视频在看本篇题解,更有助于大家对本题的理解**。
## 思路
@@ -234,7 +234,7 @@ func findMax(num1 int, num2 int) int {
}
```
-### Javascript:
+### Javascript
```Javascript
var candy = function(ratings) {
let candys = new Array(ratings.length).fill(1)
@@ -376,3 +376,4 @@ object Solution {
+
diff --git a/problems/0139.单词拆分.md b/problems/0139.单词拆分.md
index 0d88ba36..d93288ae 100644
--- a/problems/0139.单词拆分.md
+++ b/problems/0139.单词拆分.md
@@ -33,9 +33,9 @@
* 输入: s = "catsandog", wordDict = ["cats", "dog", "sand", "and", "cat"]
* 输出: false
-# 算法公开课
+## 算法公开课
-**《代码随想录》算法视频公开课:[你的背包如何装满?| LeetCode:139.单词拆分](https://www.bilibili.com/video/BV1pd4y147Rh/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
+**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[你的背包如何装满?| LeetCode:139.单词拆分](https://www.bilibili.com/video/BV1pd4y147Rh/),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
## 思路
@@ -123,7 +123,7 @@ public:
**这个代码就可以AC了,当然回溯算法不是本题的主菜,背包才是!**
-## 背包问题
+### 背包问题
单词就是物品,字符串s就是背包,单词能否组成字符串s,就是问物品能不能把背包装满。
@@ -239,7 +239,7 @@ public:
}
};
-```
+```
使用用例:s = "applepenapple", wordDict = ["apple", "pen"],对应的dp数组状态如下:
@@ -259,8 +259,8 @@ public:
## 其他语言版本
+### Java:
-Java:
```java
class Solution {
public boolean wordBreak(String s, List wordDict) {
@@ -335,7 +335,7 @@ class Solution {
}
```
-Python:
+### Python:
回溯
```python
@@ -397,7 +397,8 @@ class Solution:
-Go:
+### Go:
+
```Go
func wordBreak(s string,wordDict []string) bool {
wordDictSet := make(map[string]bool)
@@ -433,7 +434,8 @@ func wordBreak(s string, wordDict []string) bool {
}
```
-Javascript:
+### JavaScript:
+
```javascript
const wordBreak = (s, wordDict) => {
@@ -454,7 +456,7 @@ const wordBreak = (s, wordDict) => {
}
```
-TypeScript:
+### TypeScript:
> 动态规划
@@ -496,7 +498,7 @@ function wordBreak(s: string, wordDict: string[]): boolean {
};
```
-Rust:
+### Rust:
```rust
impl Solution {
@@ -519,3 +521,4 @@ impl Solution {
+
diff --git a/problems/0141.环形链表.md b/problems/0141.环形链表.md
index 7d7121a0..b1f42ba9 100644
--- a/problems/0141.环形链表.md
+++ b/problems/0141.环形链表.md
@@ -70,7 +70,7 @@ public:
## 其他语言版本
-### Java
+### Java:
```java
public class Solution {
@@ -90,7 +90,7 @@ public class Solution {
}
```
-### Python
+### Python:
```python
class Solution:
@@ -105,7 +105,7 @@ class Solution:
return False
```
-### Go
+### Go:
```go
func hasCycle(head *ListNode) bool {
@@ -125,7 +125,7 @@ func hasCycle(head *ListNode) bool {
}
```
-### JavaScript
+### JavaScript:
```js
var hasCycle = function(head) {
@@ -141,7 +141,7 @@ var hasCycle = function(head) {
};
```
-### TypeScript
+### TypeScript:
```typescript
function hasCycle(head: ListNode | null): boolean {
@@ -163,3 +163,4 @@ function hasCycle(head: ListNode | null): boolean {
+
diff --git a/problems/0142.环形链表II.md b/problems/0142.环形链表II.md
index f87d2cd9..d20101a7 100644
--- a/problems/0142.环形链表II.md
+++ b/problems/0142.环形链表II.md
@@ -11,7 +11,7 @@
> 找到有没有环已经很不容易了,还要让我找到环的入口?
-## 142.环形链表II
+# 142.环形链表II
[力扣题目链接](https://leetcode.cn/problems/linked-list-cycle-ii/)
@@ -24,9 +24,11 @@

-## 思路
+## 算法公开课
-《代码随想录》算法公开课:[把环形链表讲清楚!| LeetCode:142.环形链表II](https://www.bilibili.com/video/BV1if4y1d7ob),相信结合视频在看本篇题解,更有助于大家对链表的理解。
+**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[把环形链表讲清楚!| LeetCode:142.环形链表II](https://www.bilibili.com/video/BV1if4y1d7ob),相信结合视频在看本篇题解,更有助于大家对链表的理解。**
+
+## 思路
这道题目,不仅考察对链表的操作,而且还需要一些数学运算。
@@ -148,7 +150,7 @@ public:
* 时间复杂度: O(n),快慢指针相遇前,指针走的次数小于链表长度,快慢指针相遇后,两个index指针走的次数也小于链表长度,总体为走的次数小于 2n
* 空间复杂度: O(1)
-## 补充
+### 补充
在推理过程中,大家可能有一个疑问就是:**为什么第一次在环中相遇,slow的 步数 是 x+y 而不是 x + 若干环的长度 + y 呢?**
@@ -190,8 +192,7 @@ public:
## 其他语言版本
-
-Java:
+### Java:
```java
public class Solution {
@@ -217,8 +218,7 @@ public class Solution {
}
```
-
-Python:
+### Python:
```python
(版本一)快慢指针法
@@ -270,7 +270,7 @@ class Solution:
return None
```
-Go:
+### Go:
```go
func detectCycle(head *ListNode) *ListNode {
@@ -290,7 +290,7 @@ func detectCycle(head *ListNode) *ListNode {
}
```
-javaScript
+### JavaScript
```js
// 两种循环实现方式
@@ -334,7 +334,7 @@ var detectCycle = function(head) {
};
```
-TypeScript:
+### TypeScript:
```typescript
function detectCycle(head: ListNode | null): ListNode | null {
@@ -356,7 +356,7 @@ function detectCycle(head: ListNode | null): ListNode | null {
};
```
-Swift:
+### Swift:
```swift
class Solution {
@@ -391,7 +391,7 @@ extension ListNode: Equatable {
}
```
-C:
+### C:
```c
ListNode *detectCycle(ListNode *head) {
@@ -410,7 +410,7 @@ ListNode *detectCycle(ListNode *head) {
}
```
-Scala:
+### Scala:
```scala
object Solution {
@@ -437,7 +437,7 @@ object Solution {
}
```
-C#:
+### C#:
```CSharp
public class Solution
{
diff --git a/problems/0150.逆波兰表达式求值.md b/problems/0150.逆波兰表达式求值.md
index 09cc4f96..663a68ea 100644
--- a/problems/0150.逆波兰表达式求值.md
+++ b/problems/0150.逆波兰表达式求值.md
@@ -5,8 +5,6 @@
参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!
-
-
> 这不仅仅是一道好题,也展现出计算机的思考方式
# 150. 逆波兰表达式求值
@@ -63,9 +61,13 @@
* 适合用栈操作运算:遇到数字则入栈;遇到运算符则取出栈顶两个数字进行计算,并将结果压入栈中。
-# 思路
+## 算法公开课
-《代码随想录》算法视频公开课:[栈的最后表演! | LeetCode:150. 逆波兰表达式求值](https://www.bilibili.com/video/BV1kd4y1o7on),相信结合视频再看本篇题解,更有助于大家对本题的理解。
+**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[栈的最后表演! | LeetCode:150. 逆波兰表达式求值](https://www.bilibili.com/video/BV1kd4y1o7on),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
+
+## 思路
+
+### 正题
在上一篇文章中[1047.删除字符串中的所有相邻重复项](https://programmercarl.com/1047.删除字符串中的所有相邻重复项.html)提到了 递归就是用栈来实现的。
@@ -117,7 +119,7 @@ public:
* 空间复杂度: O(n)
-## 题外话
+### 题外话
我们习惯看到的表达式都是中缀表达式,因为符合我们的习惯,但是中缀表达式对于计算机来说就不是很友好了。
@@ -134,11 +136,9 @@ public:
> During the 1970s and 1980s, Hewlett-Packard used RPN in all of their desktop and hand-held calculators, and continued to use it in some models into the 2020s.
-
-
## 其他语言版本
-java:
+### Java:
```Java
class Solution {
@@ -164,7 +164,7 @@ class Solution {
}
```
-python3
+### Python3:
```python
from operator import add, sub, mul
@@ -201,7 +201,8 @@ class Solution:
```
-Go:
+### Go:
+
```Go
func evalRPN(tokens []string) int {
stack := []int{}
@@ -228,7 +229,7 @@ func evalRPN(tokens []string) int {
}
```
-javaScript:
+### JavaScript:
```js
var evalRPN = function (tokens) {
@@ -259,7 +260,7 @@ var evalRPN = function (tokens) {
};
```
-TypeScript:
+### TypeScript:
普通版:
@@ -324,7 +325,8 @@ function evalRPN(tokens: string[]): number {
};
```
-Swift:
+### Swift:
+
```Swift
func evalRPN(_ tokens: [String]) -> Int {
var stack = [Int]()
@@ -357,7 +359,8 @@ func evalRPN(_ tokens: [String]) -> Int {
}
```
-C#:
+### C#:
+
```csharp
public int EvalRPN(string[] tokens) {
int num;
@@ -391,8 +394,8 @@ public int EvalRPN(string[] tokens) {
}
```
+### PHP:
-PHP:
```php
class Solution {
function evalRPN($tokens) {
@@ -417,7 +420,8 @@ class Solution {
}
```
-Scala:
+### Scala:
+
```scala
object Solution {
import scala.collection.mutable
@@ -447,7 +451,7 @@ object Solution {
}
```
-rust:
+### Rust:
```rust
impl Solution {
diff --git a/problems/0151.翻转字符串里的单词.md b/problems/0151.翻转字符串里的单词.md
index 6dd3cd49..0c1a526f 100644
--- a/problems/0151.翻转字符串里的单词.md
+++ b/problems/0151.翻转字符串里的单词.md
@@ -28,10 +28,11 @@
输出: "example good a"
解释: 如果两个单词间有多余的空格,将反转后单词间的空格减少到只含一个。
+## 算法公开课
-# 思路
+**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[字符串复杂操作拿捏了! | LeetCode:151.翻转字符串里的单词](https://www.bilibili.com/video/BV1uT41177fX),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
-针对本题,我录制了视频讲解:[字符串复杂操作拿捏了! | LeetCode:151.翻转字符串里的单词](https://www.bilibili.com/video/BV1uT41177fX),结合本题解一起看,事半功倍!
+## 思路
**这道题目可以说是综合考察了字符串的多种操作。**
@@ -204,8 +205,7 @@ public:
## 其他语言版本
-
-Java:
+### Java:
```Java
class Solution {
@@ -433,9 +433,10 @@ class Solution {
}
```
-python:
+### Python:
(版本一)先删除空白,然后整个反转,最后单词反转。
**因为字符串是不可变类型,所以反转单词的时候,需要将其转换成列表,然后通过join函数再将其转换成列表,所以空间复杂度不是O(1)**
+
```Python
class Solution:
def reverseWords(self, s: str) -> str:
@@ -467,7 +468,7 @@ class Solution:
return " ".join(words)
```
-Go:
+### Go:
版本一:
@@ -546,32 +547,35 @@ func reverseWords(s string) string {
b = b[:slowIndex]
}
//2.反转整个字符串
- reverse(&b, 0, len(b)-1)
+ reverse(b)
//3.反转单个单词 i单词开始位置,j单词结束位置
i := 0
for i < len(b) {
j := i
for ; j < len(b) && b[j] != ' '; j++ {
}
- reverse(&b, i, j-1)
+ reverse(b[i:j])
i = j
i++
}
return string(b)
}
-func reverse(b *[]byte, left, right int) {
- for left < right {
- (*b)[left], (*b)[right] = (*b)[right], (*b)[left]
- left++
- right--
- }
+func reverse(b []byte) {
+ left := 0
+ right := len(b) - 1
+ for left < right {
+ b[left], b[right] = b[right], b[left]
+ left++
+ right--
+ }
}
```
-javaScript:
+### JavaScript:
+
```js
/**
* @param {string} s
@@ -630,7 +634,7 @@ function reverse(strArr, start, end) {
}
```
-TypeScript:
+### TypeScript:
```typescript
function reverseWords(s: string): string {
@@ -689,7 +693,7 @@ function reverseWords(s: string): string {
};
```
-Swift:
+### Swift:
```swift
func reverseWords(_ s: String) -> String {
@@ -766,7 +770,7 @@ func reverseWord(_ s: inout [Character]) {
}
```
-Scala:
+### Scala:
```scala
object Solution {
@@ -824,8 +828,8 @@ object Solution {
}
```
+### PHP:
-PHP:
```php
function reverseWords($s) {
$this->removeExtraSpaces($s);
@@ -872,7 +876,7 @@ function reverseString(&$s, $start, $end) {
return ;
}
```
-Rust:
+### Rust:
```Rust
// 根据C++版本二思路进行实现
@@ -924,7 +928,7 @@ pub fn remove_extra_spaces(s: &mut Vec) {
}
}
```
-C:
+### C:
```C
// 翻转字符串中指定范围的字符
diff --git a/problems/0188.买卖股票的最佳时机IV.md b/problems/0188.买卖股票的最佳时机IV.md
index 773a910a..e4c5c484 100644
--- a/problems/0188.买卖股票的最佳时机IV.md
+++ b/problems/0188.买卖股票的最佳时机IV.md
@@ -31,9 +31,9 @@
* 0 <= prices.length <= 1000
* 0 <= prices[i] <= 1000
-# 算法公开课
+## 算法公开课
-**《代码随想录》算法视频公开课:[动态规划来决定最佳时机,至多可以买卖K次!| LeetCode:188.买卖股票最佳时机4](https://www.bilibili.com/video/BV16M411U7XJ),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
+**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[动态规划来决定最佳时机,至多可以买卖K次!| LeetCode:188.买卖股票最佳时机4](https://www.bilibili.com/video/BV16M411U7XJ),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
## 思路
@@ -173,7 +173,7 @@ public:
## 其他语言版本
-Java:
+### Java:
```java
// 版本一: 三维 dp数组
@@ -295,7 +295,7 @@ class Solution {
}
```
-Python:
+### Python:
版本一
@@ -329,7 +329,7 @@ class Solution:
dp[j] = max(dp[j],dp[j-1]+prices[i])
return dp[2*k]
```
-Go:
+### Go:
版本一:
@@ -404,7 +404,7 @@ func max188(a, b int) int {
}
```
-Javascript:
+### JavaScript:
```javascript
// 方法一:动态规划
@@ -454,7 +454,7 @@ var maxProfit = function(k, prices) {
};
```
-TypeScript:
+### TypeScript:
```typescript
function maxProfit(k: number, prices: number[]): number {
@@ -474,7 +474,7 @@ function maxProfit(k: number, prices: number[]): number {
};
```
-Rust:
+### Rust:
```rust
impl Solution {
diff --git a/problems/0189.旋转数组.md b/problems/0189.旋转数组.md
index 35819694..d60612e9 100644
--- a/problems/0189.旋转数组.md
+++ b/problems/0189.旋转数组.md
@@ -33,7 +33,7 @@
向右旋转 2 步: [3,99,-1,-100]。
-# 思路
+## 思路
这道题目在字符串里其实很常见,我把字符串反转相关的题目列一下:
@@ -83,9 +83,9 @@ public:
```
-# 其他语言版本
+## 其他语言版本
-## Java
+### Java
```java
class Solution {
@@ -106,7 +106,7 @@ class Solution {
}
```
-## Python
+### Python
方法一:局部翻转 + 整体翻转
```python
@@ -139,7 +139,7 @@ class Solution:
# 备注:这个方法会导致空间复杂度变成 O(n) 因为我们要创建一个 copy 数组。但是不失为一种思路。
```
-## Go
+### Go
```go
func rotate(nums []int, k int) {
@@ -157,7 +157,7 @@ func reverse(nums []int){
}
```
-## JavaScript
+### JavaScript
```js
var rotate = function (nums, k) {
@@ -178,7 +178,7 @@ var rotate = function (nums, k) {
};
```
-## TypeScript
+### TypeScript
```typescript
function rotate(nums: number[], k: number): void {
@@ -205,3 +205,4 @@ function reverseByRange(nums: number[], left: number, right: number): void {
+
diff --git a/problems/0198.打家劫舍.md b/problems/0198.打家劫舍.md
index 80902559..a7bc4c99 100644
--- a/problems/0198.打家劫舍.md
+++ b/problems/0198.打家劫舍.md
@@ -31,9 +31,9 @@
* 0 <= nums.length <= 100
* 0 <= nums[i] <= 400
-# 算法公开课
+## 算法公开课
-**《代码随想录》算法视频公开课:[动态规划,偷不偷这个房间呢?| LeetCode:198.打家劫舍](https://www.bilibili.com/video/BV1Te411N7SX),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
+**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[动态规划,偷不偷这个房间呢?| LeetCode:198.打家劫舍](https://www.bilibili.com/video/BV1Te411N7SX),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
## 思路
@@ -121,8 +121,8 @@ public:
## 其他语言版本
+### Java:
-Java:
```Java
// 动态规划
class Solution {
@@ -194,7 +194,7 @@ class Solution {
}
```
-Python:
+### Python:
1维DP
```python
@@ -255,7 +255,8 @@ class Solution:
```
-Go:
+### Go:
+
```Go
func rob(nums []int) int {
n := len(nums)
@@ -275,7 +276,7 @@ func max(a, b int) int {
}
```
-JavaScript:
+### JavaScript:
```javascript
const rob = nums => {
@@ -291,7 +292,7 @@ const rob = nums => {
};
```
-TypeScript:
+### TypeScript:
```typescript
function rob(nums: number[]): number {
@@ -314,7 +315,7 @@ function rob(nums: number[]): number {
};
```
-Rust:
+### Rust:
```rust
impl Solution {
@@ -338,4 +339,3 @@ impl Solution {
-
diff --git a/problems/0200.岛屿数量.广搜版.md b/problems/0200.岛屿数量.广搜版.md
index c20fe4f1..cd3ae70d 100644
--- a/problems/0200.岛屿数量.广搜版.md
+++ b/problems/0200.岛屿数量.广搜版.md
@@ -197,7 +197,6 @@ class Solution {
}
```
-## 其他语言版本
### Python
BFS solution
```python
@@ -244,3 +243,4 @@ class Solution:
```
+
diff --git a/problems/0202.快乐数.md b/problems/0202.快乐数.md
index 7fe8cd8d..4a77e2b6 100644
--- a/problems/0202.快乐数.md
+++ b/problems/0202.快乐数.md
@@ -28,7 +28,7 @@
6^2 + 8^2 = 100
1^2 + 0^2 + 0^2 = 1
-# 思路
+## 思路
这道题目看上去貌似一道数学问题,其实并不是!
@@ -80,10 +80,10 @@ public:
-# 其他语言版本
+## 其他语言版本
+### Java:
-Java:
```java
class Solution {
public boolean isHappy(int n) {
@@ -107,8 +107,9 @@ class Solution {
}
```
-Python:
+### Python:
(版本一)使用集合
+
```python
class Solution:
def isHappy(self, n: int) -> bool:
@@ -131,7 +132,7 @@ class Solution:
n, r = divmod(n, 10)
new_num += r ** 2
return new_num
- ```
+```
(版本二)使用集合
```python
class Solution:
@@ -146,7 +147,7 @@ class Solution:
if new_num==1: return True
else: n = new_num
return False
-```
+ ```
(版本三)使用数组
```python
class Solution:
@@ -161,7 +162,7 @@ class Solution:
if new_num==1: return True
else: n = new_num
return False
-```
+ ```
(版本四)使用快慢指针
```python
class Solution:
@@ -180,7 +181,7 @@ class Solution:
n, r = divmod(n, 10)
new_num += r ** 2
return new_num
-```
+ ```
(版本五)使用集合+精简
```python
class Solution:
@@ -192,7 +193,7 @@ class Solution:
return False
seen.add(n)
return True
-```
+ ```
(版本六)使用数组+精简
```python
class Solution:
@@ -204,8 +205,9 @@ class Solution:
return False
seen.append(n)
return True
-```
-Go:
+ ```
+### Go:
+
```go
func isHappy(n int) bool {
m := make(map[int]bool)
@@ -225,7 +227,7 @@ func getSum(n int) int {
}
```
-javaScript:
+### JavaScript:
```js
var isHappy = function (n) {
@@ -303,7 +305,7 @@ var isHappy = function(n) {
};
```
-TypeScript:
+### TypeScript:
```typescript
function isHappy(n: number): boolean {
@@ -322,7 +324,7 @@ function isHappy(n: number): boolean {
};
```
-Swift:
+### Swift:
```swift
// number 每个位置上的数字的平方和
@@ -355,7 +357,8 @@ func isHappy(_ n: Int) -> Bool {
}
```
-PHP:
+### PHP:
+
```php
class Solution {
/**
@@ -386,7 +389,8 @@ class Solution {
}
```
-Rust:
+### Rust:
+
```Rust
use std::collections::HashSet;
impl Solution {
@@ -416,7 +420,8 @@ impl Solution {
}
```
-C:
+### C:
+
```C
typedef struct HashNodeTag {
int key; /* num */
@@ -473,8 +478,8 @@ object Solution {
}
```
+### C#:
-C#:
```csharp
public class Solution {
private int getSum(int n) {
@@ -500,3 +505,4 @@ public class Solution {
+
diff --git a/problems/0203.移除链表元素.md b/problems/0203.移除链表元素.md
index 300f98e9..c8f802a1 100644
--- a/problems/0203.移除链表元素.md
+++ b/problems/0203.移除链表元素.md
@@ -27,14 +27,12 @@
输入:head = [7,7,7,7], val = 7
输出:[]
-# 算法公开课
+## 算法公开课
**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[链表基础操作| LeetCode:203.移除链表元素](https://www.bilibili.com/video/BV18B4y1s7R9),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
-# 思路
-
-为了方便大家理解,我特意录制了视频:[链表基础操作| LeetCode:203.移除链表元素](https://www.bilibili.com/video/BV18B4y1s7R9),结合视频在看本题解,事半功倍。
+## 思路
这里以链表 1 4 2 4 来举例,移除元素4。
@@ -90,9 +88,6 @@
最后呢在题目中,return 头结点的时候,别忘了 `return dummyNode->next;`, 这才是新的头结点
-
-# C++代码
-
**直接使用原来的链表来进行移除节点操作:**
```CPP
@@ -159,7 +154,7 @@ public:
## 其他语言版本
-C:
+### C:
用原来的链表操作:
```c
@@ -227,7 +222,7 @@ struct ListNode* removeElements(struct ListNode* head, int val){
}
```
-Java:
+### Java:
```java
/**
@@ -308,7 +303,7 @@ public ListNode removeElements(ListNode head, int val) {
}
```
-Python:
+### Python:
```python
(版本一)虚拟头节点法
@@ -334,7 +329,7 @@ class Solution:
```
-Go:
+### Go:
```go
/**
@@ -359,7 +354,7 @@ func removeElements(head *ListNode, val int) *ListNode {
}
```
-javaScript:
+### JavaScript:
```js
/**
@@ -381,7 +376,7 @@ var removeElements = function(head, val) {
};
```
-TypeScript:
+### TypeScript:
版本一(在原链表上直接删除):
@@ -437,7 +432,7 @@ function removeElements(head: ListNode | null, val: number): ListNode | null {
};
```
-Swift:
+### Swift:
```swift
/**
@@ -465,7 +460,7 @@ func removeElements(_ head: ListNode?, _ val: Int) -> ListNode? {
}
```
-PHP:
+### PHP:
```php
/**
@@ -493,7 +488,7 @@ func removeElements(head *ListNode, val int) *ListNode {
}
```
-RUST:
+### Rust:
```rust
// Definition for singly-linked list.
@@ -531,7 +526,7 @@ impl Solution {
}
```
-Scala:
+### Scala:
```scala
/**
@@ -564,7 +559,7 @@ object Solution {
}
```
-Kotlin:
+### Kotlin:
```kotlin
/**
@@ -600,7 +595,8 @@ class Solution {
}
```
-C#
+### C#
+
```CSharp
/**
* Definition for singly-linked list.
@@ -639,3 +635,4 @@ public class Solution
+
diff --git a/problems/0205.同构字符串.md b/problems/0205.同构字符串.md
index a507638c..e07ab746 100644
--- a/problems/0205.同构字符串.md
+++ b/problems/0205.同构字符串.md
@@ -29,7 +29,7 @@
提示:可以假设 s 和 t 长度相同。
-# 思路
+## 思路
字符串没有说都是小写字母之类的,所以用数组不合适了,用map来做映射。
@@ -61,9 +61,9 @@ public:
```
-# 其他语言版本
+## 其他语言版本
-## Java
+### Java
```java
class Solution {
@@ -87,7 +87,7 @@ class Solution {
}
```
-## Python
+### Python
```python
class Solution:
@@ -110,7 +110,7 @@ class Solution:
return True
```
-## Go
+### Go
```go
func isIsomorphic(s string, t string) bool {
@@ -132,7 +132,7 @@ func isIsomorphic(s string, t string) bool {
}
```
-## JavaScript
+### JavaScript
```js
var isIsomorphic = function(s, t) {
@@ -156,7 +156,7 @@ var isIsomorphic = function(s, t) {
};
```
-## TypeScript
+### TypeScript
```typescript
function isIsomorphic(s: string, t: string): boolean {
@@ -183,3 +183,4 @@ function isIsomorphic(s: string, t: string): boolean {
+
diff --git a/problems/0206.翻转链表.md b/problems/0206.翻转链表.md
index c63c998d..5a57939a 100644
--- a/problems/0206.翻转链表.md
+++ b/problems/0206.翻转链表.md
@@ -17,14 +17,12 @@
输入: 1->2->3->4->5->NULL
输出: 5->4->3->2->1->NULL
-# 算法公开课
+## 算法公开课
**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[帮你拿下反转链表 | LeetCode:206.反转链表](https://www.bilibili.com/video/BV1nB4y1i7eL),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
-# 思路
-
-本题我录制了B站视频,[帮你拿下反转链表 | LeetCode:206.反转链表](https://www.bilibili.com/video/BV1nB4y1i7eL),相信结合视频在看本篇题解,更有助于大家对链表的理解。
+## 思路
如果再定义一个新的链表,实现链表元素的反转,其实这是对内存空间的浪费。
@@ -51,9 +49,7 @@
最后,cur 指针已经指向了null,循环结束,链表也反转完毕了。 此时我们return pre指针就可以了,pre指针就指向了新的头结点。
-# C++代码
-
-## 双指针法
+### 双指针法
```CPP
class Solution {
public:
@@ -76,7 +72,7 @@ public:
* 时间复杂度: O(n)
* 空间复杂度: O(1)
-## 递归法
+### 递归法
递归法相对抽象一些,但是其实和双指针法是一样的逻辑,同样是当cur为空的时候循环结束,不断将cur指向pre的过程。
@@ -137,8 +133,8 @@ public:
## 其他语言版本
+### Java:
-Java:
```java
// 双指针
class Solution {
@@ -198,7 +194,8 @@ class Solution {
}
```
-Python
+### Python:
+
```python
(版本一)双指针法
# Definition for singly-linked list.
@@ -219,8 +216,6 @@ class Solution:
return pre
```
-Python递归法:
-
```python
(版本二)递归法
# Definition for singly-linked list.
@@ -242,7 +237,7 @@ class Solution:
-Go:
+### Go:
```go
//双指针
@@ -273,7 +268,7 @@ func help(pre, head *ListNode)*ListNode{
}
```
-javaScript:
+### JavaScript:
```js
/**
@@ -328,7 +323,7 @@ var reverseList = function(head) {
};
```
-TypeScript:
+### TypeScript:
```typescript
// 双指针法
@@ -376,7 +371,7 @@ function reverseList(head: ListNode | null): ListNode | null {
};
```
-Ruby:
+### Ruby:
```ruby
# 双指针
@@ -421,7 +416,8 @@ def reverse(pre, cur)
end
```
-Kotlin:
+### Kotlin:
+
```Kotlin
fun reverseList(head: ListNode?): ListNode? {
var pre: ListNode? = null
@@ -471,7 +467,8 @@ class Solution {
}
```
-Swift:
+### Swift:
+
```swift
/// 双指针法 (迭代)
/// - Parameter head: 头结点
@@ -508,8 +505,9 @@ func reverse(pre: ListNode?, cur: ListNode?) -> ListNode? {
}
```
-C:
+### C:
双指针法:
+
```c
struct ListNode* reverseList(struct ListNode* head){
//保存cur的下一个结点
@@ -549,7 +547,8 @@ struct ListNode* reverseList(struct ListNode* head){
-PHP:
+### PHP:
+
```php
// 双指针法:
function reverseList($head) {
@@ -565,8 +564,9 @@ function reverseList($head) {
}
```
-Scala:
+### Scala:
双指针法:
+
```scala
object Solution {
def reverseList(head: ListNode): ListNode = {
@@ -601,7 +601,7 @@ object Solution {
}
```
-Rust:
+### Rust:
双指针法:
```rust
@@ -640,7 +640,7 @@ impl Solution {
}
}
```
-C#:
+### C#:
三指针法, 感觉会更直观:
```cs
@@ -677,11 +677,11 @@ public class LinkNumbers
}
```
+## 其他解法
+### 使用虚拟头结点解决链表反转
-## 使用虚拟头结点解决链表翻转
-
-> 使用虚拟头结点,通过头插法实现链表的翻转(不需要栈)
+> 使用虚拟头结点,通过头插法实现链表的反转(不需要栈)
```java
// 迭代方法:增加虚头结点,使用头插法实现链表翻转
@@ -704,7 +704,7 @@ public static ListNode reverseList1(ListNode head) {
-## 使用栈解决反转链表的问题
+### 使用栈解决反转链表的问题
* 首先将所有的结点入栈
* 然后创建一个虚拟虚拟头结点,让cur指向虚拟头结点。然后开始循环出栈,每出来一个元素,就把它加入到以虚拟头结点为头结点的链表当中,最后返回即可。
@@ -743,4 +743,3 @@ public ListNode reverseList(ListNode head) {
-
diff --git a/problems/0213.打家劫舍II.md b/problems/0213.打家劫舍II.md
index ee62b574..cd9d596d 100644
--- a/problems/0213.打家劫舍II.md
+++ b/problems/0213.打家劫舍II.md
@@ -31,9 +31,9 @@
* 1 <= nums.length <= 100
* 0 <= nums[i] <= 1000
-# 算法公开课
+## 算法公开课
-**《代码随想录》算法视频公开课:[动态规划,房间连成环了那还偷不偷呢?| LeetCode:213.打家劫舍II](https://www.bilibili.com/video/BV1oM411B7xq),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
+**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[动态规划,房间连成环了那还偷不偷呢?| LeetCode:213.打家劫舍II](https://www.bilibili.com/video/BV1oM411B7xq),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
## 思路
@@ -104,8 +104,8 @@ public:
## 其他语言版本
+### Java:
-Java:
```Java
class Solution {
public int rob(int[] nums) {
@@ -129,7 +129,7 @@ class Solution {
}
```
-Python:
+### Python:
```Python
class Solution:
@@ -219,7 +219,7 @@ class Solution:
```
-Go:
+### Go:
```go
// 打家劫舍Ⅱ 动态规划
@@ -257,7 +257,8 @@ func max(a, b int) int {
}
```
-javascipt:
+### JavaScript:
+
```javascript
var rob = function(nums) {
const n = nums.length
@@ -279,7 +280,7 @@ const robRange = (nums, start, end) => {
return dp[end]
}
```
-TypeScript:
+### TypeScript:
```typescript
function rob(nums: number[]): number {
@@ -301,7 +302,7 @@ function robRange(nums: number[], start: number, end: number): number {
}
```
-Rust:
+### Rust:
```rust
impl Solution {
@@ -336,3 +337,4 @@ impl Solution {
+
diff --git a/problems/0216.组合总和III.md b/problems/0216.组合总和III.md
index 319b2eba..4de7dc58 100644
--- a/problems/0216.组合总和III.md
+++ b/problems/0216.组合总和III.md
@@ -7,8 +7,6 @@
-
-
> 别看本篇选的是组合总和III,而不是组合总和,本题和上一篇77.组合相比难度刚刚好!
# 216.组合总和III
@@ -30,12 +28,12 @@
输入: k = 3, n = 9
输出: [[1,2,6], [1,3,5], [2,3,4]]
-# 算法公开课
+## 算法公开课
-**《代码随想录》算法视频公开课:[和组合问题有啥区别?回溯算法如何剪枝?| LeetCode:216.组合总和III](https://www.bilibili.com/video/BV1wg411873x),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
+**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[和组合问题有啥区别?回溯算法如何剪枝?| LeetCode:216.组合总和III](https://www.bilibili.com/video/BV1wg411873x),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
-# 思路
+## 思路
本题就是在[1,2,3,4,5,6,7,8,9]这个集合中找到和为n的k个数的组合。
@@ -54,7 +52,7 @@
图中,可以看出,只有最后取到集合(1,3)和为4 符合条件。
-## 回溯三部曲
+### 回溯三部曲
* **确定递归函数参数**
@@ -165,7 +163,7 @@ public:
};
```
-## 剪枝
+### 剪枝
这道题目,剪枝操作其实是很容易想到了,想必大家看上面的树形图的时候已经想到了。
@@ -238,7 +236,7 @@ public:
* 时间复杂度: O(n * 2^n)
* 空间复杂度: O(n)
-# 总结
+## 总结
开篇就介绍了本题与[77.组合](https://programmercarl.com/0077.组合.html)的区别,相对来说加了元素总和的限制,如果做完[77.组合](https://programmercarl.com/0077.组合.html)再做本题在合适不过。
@@ -249,10 +247,10 @@ public:
-# 其他语言版本
+## 其他语言版本
-## Java
+### Java
模板方法
@@ -358,7 +356,7 @@ class Solution {
}
```
-## Python
+### Python
```py
class Solution:
@@ -383,7 +381,7 @@ class Solution:
```
-## Go
+### Go
回溯+减枝
@@ -418,7 +416,7 @@ func dfs(k, n int, start int, sum int) {
}
```
-## javaScript
+### JavaScript
```js
/**
@@ -455,7 +453,7 @@ var combinationSum3 = function(k, n) {
};
```
-## TypeScript
+### TypeScript
```typescript
function combinationSum3(k: number, n: number): number[][] {
@@ -479,7 +477,7 @@ function combinationSum3(k: number, n: number): number[][] {
};
```
-## Rust
+### Rust
```Rust
impl Solution {
@@ -516,7 +514,7 @@ impl Solution {
}
```
-## C
+### C
```c
int* path;
@@ -575,7 +573,7 @@ int** combinationSum3(int k, int n, int* returnSize, int** returnColumnSizes){
}
```
-## Swift
+### Swift
```swift
func combinationSum3(_ count: Int, _ targetSum: Int) -> [[Int]] {
@@ -607,7 +605,7 @@ func combinationSum3(_ count: Int, _ targetSum: Int) -> [[Int]] {
}
```
-## Scala
+### Scala
```scala
object Solution {
diff --git a/problems/0222.完全二叉树的节点个数.md b/problems/0222.完全二叉树的节点个数.md
index 795a6f37..d54f9b85 100644
--- a/problems/0222.完全二叉树的节点个数.md
+++ b/problems/0222.完全二叉树的节点个数.md
@@ -29,14 +29,17 @@
* 0 <= Node.val <= 5 * 10^4
* 题目数据保证输入的树是 完全二叉树
+## 算法公开课
-# 思路
+**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[要理解普通二叉树和完全二叉树的区别! | LeetCode:222.完全二叉树节点的数量](https://www.bilibili.com/video/BV1eW4y1B7pD),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
+
+
+## 思路
-《代码随想录》算法视频公开课:[要理解普通二叉树和完全二叉树的区别! | LeetCode:222.完全二叉树节点的数量](https://www.bilibili.com/video/BV1eW4y1B7pD),相信结合视频在看本篇题解,更有助于大家对本题的理解。
本篇给出按照普通二叉树的求法以及利用完全二叉树性质的求法。
-## 普通二叉树
+### 普通二叉树
首先按照普通二叉树的逻辑来求。
@@ -44,7 +47,7 @@
递归遍历的顺序依然是后序(左右中)。
-### 递归
+#### 递归
如果对求二叉树深度还不熟悉的话,看这篇:[二叉树:看看这些树的最大深度](https://programmercarl.com/0104.二叉树的最大深度.html)。
@@ -112,7 +115,7 @@ public:
**网上基本都是这个精简的代码版本,其实不建议大家照着这个来写,代码确实精简,但隐藏了一些内容,连遍历的顺序都看不出来,所以初学者建议学习版本一的代码,稳稳的打基础**。
-### 迭代法
+#### 迭代
如果对求二叉树层序遍历还不熟悉的话,看这篇:[二叉树:层序遍历登场!](https://programmercarl.com/0102.二叉树的层序遍历.html)。
@@ -142,7 +145,7 @@ public:
* 时间复杂度:O(n)
* 空间复杂度:O(n)
-## 完全二叉树
+### 完全二叉树
以上方法都是按照普通二叉树来做的,对于完全二叉树特性不了解的同学可以看这篇 [关于二叉树,你该了解这些!](https://programmercarl.com/二叉树理论基础.html),这篇详细介绍了各种二叉树的特性。
@@ -249,9 +252,9 @@ public:
* 时间复杂度:O(log n × log n)
* 空间复杂度:O(log n)
-# 其他语言版本
+## 其他语言版本
-## Java
+### Java:
```java
class Solution {
// 通用递归解法
@@ -312,7 +315,7 @@ class Solution {
}
```
-## Python
+### Python:
递归法:
```python
@@ -408,7 +411,7 @@ class Solution: # 利用完全二叉树特性
return 1+self.countNodes(root.left)+self.countNodes(root.right)
```
-## Go
+### Go:
递归版本
@@ -488,9 +491,7 @@ func countNodes(root *TreeNode) int {
}
```
-
-
-## JavaScript:
+### JavaScript:
递归版本
```javascript
@@ -559,7 +560,7 @@ var countNodes = function(root) {
};
```
-## TypeScrpt:
+### TypeScrpt:
> 递归法
@@ -614,7 +615,7 @@ function countNodes(root: TreeNode | null): number {
};
```
-## C:
+### C:
递归法
```c
@@ -690,7 +691,7 @@ int countNodes(struct TreeNode* root){
}
```
-## Swift:
+### Swift:
> 递归
```swift
@@ -758,7 +759,7 @@ func countNodes(_ root: TreeNode?) -> Int {
}
```
-## Scala
+### Scala:
递归:
```scala
@@ -821,9 +822,9 @@ object Solution {
}
```
-rust:
+### Rust:
-// 递归
+递归
```rust
use std::cell::RefCell;
use std::rc::Rc;
@@ -838,7 +839,7 @@ impl Solution {
}
```
-// 迭代
+迭代
```rust
use std::rc::Rc;
use std::cell::RefCell;
diff --git a/problems/0225.用队列实现栈.md b/problems/0225.用队列实现栈.md
index 94c79404..13b742f8 100644
--- a/problems/0225.用队列实现栈.md
+++ b/problems/0225.用队列实现栈.md
@@ -25,11 +25,11 @@
* 你所使用的语言也许不支持队列。 你可以使用 list 或者 deque(双端队列)来模拟一个队列 , 只要是标准的队列操作即可。
* 你可以假设所有操作都是有效的(例如, 对一个空的栈不会调用 pop 或者 top 操作)。
+## 算法公开课
-# 思路
+**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[队列的基本操作! | LeetCode:225. 用队列实现栈](https://www.bilibili.com/video/BV1Fd4y1K7sm),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
-
-《代码随想录》算法公开课:[队列的基本操作! | LeetCode:225. 用队列实现栈](https://www.bilibili.com/video/BV1Fd4y1K7sm),相信结合视频再看本篇题解,更有助于大家对链表的理解。
+## 思路
(这里要强调是单向队列)
@@ -114,7 +114,7 @@ public:
* 时间复杂度: push为O(n),其他为O(1)
* 空间复杂度: O(n)
-# 优化
+## 优化
其实这道题目就是用一个队列就够了。
@@ -162,9 +162,9 @@ public:
* 空间复杂度: O(n)
-# 其他语言版本
+## 其他语言版本
-Java:
+### Java:
使用两个 Queue 实现方法1
```java
@@ -404,7 +404,7 @@ class MyStack {
}
```
-Python:
+### Python:
```python
from collections import deque
@@ -496,8 +496,7 @@ class MyStack:
return not self.que
```
-
-Go:
+### Go:
使用两个队列实现
```go
@@ -628,9 +627,7 @@ func (this *MyStack) Empty() bool {
*/
```
-
-
-javaScript:
+### JavaScript:
使用数组(push, shift)模拟队列
@@ -740,7 +737,7 @@ MyStack.prototype.empty = function() {
```
-TypeScript:
+### TypeScript:
版本一:使用两个队列模拟栈
@@ -812,7 +809,7 @@ class MyStack {
}
```
-Swift
+### Swift:
```Swift
// 定义一个队列数据结构
@@ -931,8 +928,9 @@ class MyStack {
}
}
```
-Scala:
+### Scala:
使用两个队列模拟栈:
+
```scala
import scala.collection.mutable
@@ -1015,8 +1013,8 @@ class MyStack() {
}
```
+### C#:
-C#:
```csharp
public class MyStack {
Queue queue1;
@@ -1051,8 +1049,9 @@ public class MyStack {
}
```
-PHP
-> 双对列
+### PHP:
+
+> 双队列
```php
// SplQueue 类通过使用一个双向链表来提供队列的主要功能。(PHP 5 >= 5.3.0, PHP 7, PHP 8)
// https://www.php.net/manual/zh/class.splqueue.php
@@ -1130,6 +1129,8 @@ class MyStack {
}
```
+### Rust:
+
> rust:单队列
```rust
diff --git a/problems/0226.翻转二叉树.md b/problems/0226.翻转二叉树.md
index b3aea9ed..11517783 100644
--- a/problems/0226.翻转二叉树.md
+++ b/problems/0226.翻转二叉树.md
@@ -16,7 +16,11 @@
这道题目背后有一个让程序员心酸的故事,听说 Homebrew的作者Max Howell,就是因为没在白板上写出翻转二叉树,最后被Google拒绝了。(真假不做判断,权当一个乐子哈)
-# 题外话
+## 算法公开课
+
+**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[听说一位巨佬面Google被拒了,因为没写出翻转二叉树 | LeetCode:226.翻转二叉树](https://www.bilibili.com/video/BV1sP4y1f7q7),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
+
+## 题外话
这道题目是非常经典的题目,也是比较简单的题目(至少一看就会)。
@@ -24,9 +28,7 @@
如果做过这道题的同学也建议认真看完,相信一定有所收获!
-# 思路
-
-《代码随想录》算法视频公开课:[听说一位巨佬面Google被拒了,因为没写出翻转二叉树 | LeetCode:226.翻转二叉树](https://www.bilibili.com/video/BV1sP4y1f7q7),相信结合视频在看本篇题解,更有助于大家对本题的理解。
+## 思路
我们之前介绍的都是各种方式遍历二叉树,这次要翻转了,感觉还是有点懵逼。
@@ -49,7 +51,7 @@
那么层序遍历可以不可以呢?**依然可以的!只要把每一个节点的左右孩子翻转一下的遍历方式都是可以的!**
-## 递归法
+### 递归法
对于二叉树的递归法的前中后序遍历,已经在[二叉树:前中后序递归遍历](https://programmercarl.com/二叉树的递归遍历.html)详细讲解了。
@@ -102,9 +104,9 @@ public:
};
```
-## 迭代法
+### 迭代法
-### 深度优先遍历
+#### 深度优先遍历
[二叉树:听说递归能做的,栈也能做!](https://programmercarl.com/二叉树的迭代遍历.html)中给出了前中后序迭代方式的写法,所以本题可以很轻松的写出如下迭代法的代码:
@@ -163,7 +165,7 @@ public:
如果上面这个代码看不懂,回顾一下文章[二叉树:前中后序迭代方式的统一写法](https://programmercarl.com/二叉树的统一迭代法.html)。
-### 广度优先遍历
+#### 广度优先遍历
也就是层序遍历,层数遍历也是可以翻转这棵树的,因为层序遍历也可以把每个节点的左右孩子都翻转一遍,代码如下:
@@ -259,7 +261,7 @@ public:
## 其他语言版本
-### Java
+### Java:
```Java
//DFS递归
class Solution {
@@ -310,7 +312,7 @@ class Solution {
}
```
-### Python
+### Python:
递归法:前序遍历:
```python
@@ -466,7 +468,7 @@ class Solution:
```
-### Go
+### Go:
递归版本的前序遍历
```Go
@@ -575,7 +577,7 @@ func invertTree(root *TreeNode) *TreeNode {
}
```
-### JavaScript
+### JavaScript:
使用递归版本的前序遍历
```javascript
@@ -783,7 +785,7 @@ function invertTree(root: TreeNode | null): TreeNode | null {
};
```
-### C
+### C:
递归法
```c
@@ -961,7 +963,7 @@ object Solution {
}
```
-### rust
+### Rust:
```rust
impl Solution {
@@ -991,7 +993,7 @@ impl Solution {
}
```
-### C#
+### C#:
```csharp
//递归
@@ -1042,3 +1044,4 @@ public class Solution {
+
diff --git a/problems/0232.用栈实现队列.md b/problems/0232.用栈实现队列.md
index 4a57ee96..c510fc12 100644
--- a/problems/0232.用栈实现队列.md
+++ b/problems/0232.用栈实现队列.md
@@ -36,11 +36,12 @@ queue.empty(); // 返回 false
* 你所使用的语言也许不支持栈。你可以使用 list 或者 deque(双端队列)来模拟一个栈,只要是标准的栈操作即可。
* 假设所有操作都是有效的 (例如,一个空的队列不会调用 pop 或者 peek 操作)。
+## 算法公开课
+
+**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[栈的基本操作! | LeetCode:232.用栈实现队列](https://www.bilibili.com/video/BV1nY4y1w7VC),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
+
## 思路
-《代码随想录》算法公开课:[栈的基本操作! | LeetCode:232.用栈实现队列](https://www.bilibili.com/video/BV1nY4y1w7VC),相信结合视频再看本篇题解,更有助于大家对栈和队列的理解。
-
-
这是一道模拟题,不涉及到具体算法,考察的就是对栈和队列的掌握程度。
使用栈来模式队列的行为,如果仅仅用一个栈,是一定不行的,所以需要两个栈**一个输入栈,一个输出栈**,这里要注意输入栈和输出栈的关系。
@@ -132,7 +133,7 @@ public:
## 其他语言版本
-Java:
+### Java:
```java
class MyQueue {
@@ -179,8 +180,8 @@ class MyQueue {
```
+### Python:
-Python:
```python
class MyQueue:
@@ -231,8 +232,8 @@ class MyQueue:
```
+### Go:
-Go:
```Go
type MyQueue struct {
stackIn []int //输入栈
@@ -283,7 +284,7 @@ func (this *MyQueue) Empty() bool {
}
```
- javaScript:
+### JavaScript:
```js
// 使用两个数组的栈方法(push, pop) 实现队列
@@ -338,7 +339,7 @@ MyQueue.prototype.empty = function() {
};
```
-TypeScript:
+### TypeScript:
```typescript
class MyQueue {
@@ -374,7 +375,7 @@ class MyQueue {
}
```
-Swift:
+### Swift:
```swift
class MyQueue {
@@ -413,7 +414,8 @@ class MyQueue {
}
```
-C:
+### C:
+
```C
/*
1.两个type为int的数组(栈),大小为100
@@ -490,8 +492,8 @@ void myQueueFree(MyQueue* obj) {
}
```
+### C#:
-C#:
```csharp
public class MyQueue {
Stack inStack;
@@ -534,7 +536,8 @@ public class MyQueue {
-PHP:
+### PHP:
+
```php
// SplStack 类通过使用一个双向链表来提供栈的主要功能。[PHP 5 >= 5.3.0, PHP 7, PHP 8]
// https://www.php.net/manual/zh/class.splstack.php
@@ -579,7 +582,8 @@ class MyQueue {
}
```
-Scala:
+### Scala:
+
```scala
class MyQueue() {
import scala.collection.mutable
@@ -621,7 +625,7 @@ class MyQueue() {
}
```
-rust:
+### Rust:
```rust
struct MyQueue {
@@ -666,4 +670,3 @@ impl MyQueue {
-
diff --git a/problems/0234.回文链表.md b/problems/0234.回文链表.md
index 18b397e3..fef942fc 100644
--- a/problems/0234.回文链表.md
+++ b/problems/0234.回文链表.md
@@ -432,3 +432,4 @@ function reverseList(head: ListNode | null): ListNode | null {
+
diff --git a/problems/0235.二叉搜索树的最近公共祖先.md b/problems/0235.二叉搜索树的最近公共祖先.md
index 9777bb0b..2b8af060 100644
--- a/problems/0235.二叉搜索树的最近公共祖先.md
+++ b/problems/0235.二叉搜索树的最近公共祖先.md
@@ -36,11 +36,11 @@
* 所有节点的值都是唯一的。
* p、q 为不同节点且均存在于给定的二叉搜索树中。
-# 算法公开课
+## 算法公开课
-**《代码随想录》算法视频公开课:[二叉搜索树找祖先就有点不一样了!| 235. 二叉搜索树的最近公共祖先](https://www.bilibili.com/video/BV1Zt4y1F7ww?share_source=copy_web),相信结合视频在看本篇题解,更有助于大家对本题的理解**。
+**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[二叉搜索树找祖先就有点不一样了!| 235. 二叉搜索树的最近公共祖先](https://www.bilibili.com/video/BV1Zt4y1F7ww?share_source=copy_web),相信结合视频在看本篇题解,更有助于大家对本题的理解**。
-# 思路
+## 思路
做过[二叉树:公共祖先问题](https://programmercarl.com/0236.二叉树的最近公共祖先.html)题目的同学应该知道,利用回溯从底向上搜索,遇到一个节点的左子树里有p,右子树里有q,那么当前节点就是最近公共祖先。
@@ -71,7 +71,7 @@
可以看出直接按照指定的方向,就可以找到节点8,为最近公共祖先,而且不需要遍历整棵树,找到结果直接返回!
-## 递归法
+### 递归法
递归三部曲如下:
@@ -203,7 +203,7 @@ public:
};
```
-## 迭代法
+### 迭代法
对于二叉搜索树的迭代法,大家应该在[二叉树:二叉搜索树登场!](https://programmercarl.com/0700.二叉搜索树中的搜索.html)就了解了。
@@ -229,7 +229,7 @@ public:
灵魂拷问:是不是又被简单的迭代法感动到痛哭流涕?
-# 总结
+## 总结
对于二叉搜索树的最近祖先问题,其实要比[普通二叉树公共祖先问题](https://programmercarl.com/0236.二叉树的最近公共祖先.html)简单的多。
@@ -238,10 +238,10 @@ public:
最后给出了对应的迭代法,二叉搜索树的迭代法甚至比递归更容易理解,也是因为其有序性(自带方向性),按照目标区间找就行了。
-# 其他语言版本
+## 其他语言版本
-## Java
+### Java
递归法:
```java
@@ -273,7 +273,7 @@ class Solution {
```
-## Python
+### Python
递归法(版本一)
```python
@@ -326,7 +326,7 @@ class Solution:
```
-## Go
+### Go
递归法:
```go
@@ -350,7 +350,7 @@ func lowestCommonAncestor(root, p, q *TreeNode) *TreeNode {
```
-## JavaScript
+### JavaScript
递归法:
```javascript
@@ -391,7 +391,7 @@ var lowestCommonAncestor = function(root, p, q) {
};
```
-## TypeScript
+### TypeScript
> 递归法:
@@ -422,7 +422,7 @@ function lowestCommonAncestor(root: TreeNode | null, p: TreeNode | null, q: Tree
};
```
-## Scala
+### Scala
递归:
@@ -453,7 +453,7 @@ object Solution {
}
```
-## rust
+### Rust
递归:
@@ -519,3 +519,4 @@ impl Solution {
+
diff --git a/problems/0236.二叉树的最近公共祖先.md b/problems/0236.二叉树的最近公共祖先.md
index 0ebd5566..9db7409e 100644
--- a/problems/0236.二叉树的最近公共祖先.md
+++ b/problems/0236.二叉树的最近公共祖先.md
@@ -34,12 +34,12 @@
* 所有节点的值都是唯一的。
* p、q 为不同节点且均存在于给定的二叉树中。
-# 算法公开课
+## 算法公开课
-**《代码随想录》算法视频公开课:[自底向上查找,有点难度! | LeetCode:236. 二叉树的最近公共祖先](https://www.bilibili.com/video/BV1jd4y1B7E2),相信结合视频在看本篇题解,更有助于大家对本题的理解**。
+**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[自底向上查找,有点难度! | LeetCode:236. 二叉树的最近公共祖先](https://www.bilibili.com/video/BV1jd4y1B7E2),相信结合视频在看本篇题解,更有助于大家对本题的理解**。
-# 思路
+## 思路
遇到这个题目首先想的是要是能自底向上查找就好了,这样就可以找到公共祖先了。
@@ -226,7 +226,7 @@ public:
};
```
-# 总结
+## 总结
这道题目刷过的同学未必真正了解这里面回溯的过程,以及结果是如何一层一层传上去的。
@@ -243,10 +243,10 @@ public:
本题没有给出迭代法,因为迭代法不适合模拟回溯的过程。理解递归的解法就够了。
-# 其他语言版本
+## 其他语言版本
-## Java
+### Java
```Java
class Solution {
@@ -273,7 +273,7 @@ class Solution {
```
-## Python
+### Python
递归法(版本一)
```python
class Solution:
@@ -312,7 +312,7 @@ class Solution:
return left
```
-## Go
+### Go
```Go
func lowestCommonAncestor(root, p, q *TreeNode) *TreeNode {
@@ -343,7 +343,7 @@ func lowestCommonAncestor(root, p, q *TreeNode) *TreeNode {
}
```
-## JavaScript
+### JavaScript
```javascript
var lowestCommonAncestor = function(root, p, q) {
@@ -370,7 +370,7 @@ var lowestCommonAncestor = function(root, p, q) {
};
```
-## TypeScript
+### TypeScript
```typescript
function lowestCommonAncestor(root: TreeNode | null, p: TreeNode | null, q: TreeNode | null): TreeNode | null {
@@ -384,7 +384,7 @@ function lowestCommonAncestor(root: TreeNode | null, p: TreeNode | null, q: Tree
};
```
-## Scala
+### Scala
```scala
object Solution {
@@ -404,7 +404,7 @@ object Solution {
}
```
-## rust
+### Rust
```rust
impl Solution {
@@ -436,3 +436,4 @@ impl Solution {
+
diff --git a/problems/0239.滑动窗口最大值.md b/problems/0239.滑动窗口最大值.md
index f1c4b76c..6f420479 100644
--- a/problems/0239.滑动窗口最大值.md
+++ b/problems/0239.滑动窗口最大值.md
@@ -28,11 +28,11 @@
* -10^4 <= nums[i] <= 10^4
* 1 <= k <= nums.length
+## 算法公开课
+**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[单调队列正式登场!| LeetCode:239. 滑动窗口最大值](https://www.bilibili.com/video/BV1XS4y1p7qj),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
-# 思路
-
-《代码随想录》算法视频公开课:[单调队列正式登场!| LeetCode:239. 滑动窗口最大值](https://www.bilibili.com/video/BV1XS4y1p7qj),相信结合视频在看本篇题解,更有助于大家对本题的理解。
+## 思路
这是使用单调队列的经典题目。
@@ -196,7 +196,7 @@ public:
空间复杂度因为我们定义一个辅助队列,所以是O(k)。
-# 扩展
+## 扩展
大家貌似对单调队列 都有一些疑惑,首先要明确的是,题解中单调队列里的pop和push接口,仅适用于本题哈。单调队列不是一成不变的,而是不同场景不同写法,总之要保证队列里单调递减或递增的原则,所以叫做单调队列。 不要以为本题中的单调队列实现就是固定的写法哈。
@@ -204,10 +204,10 @@ public:
-# 其他语言版本
+## 其他语言版本
+### Java:
-Java:
```Java
//解法一
//自定义数组
@@ -298,7 +298,8 @@ class Solution {
}
```
-Python:
+### Python:
+
```python
from collections import deque
@@ -338,8 +339,7 @@ class Solution:
return result
```
-
-Go:
+### Go:
```go
// 封装单调队列的方式解题
@@ -401,7 +401,8 @@ func maxSlidingWindow(nums []int, k int) []int {
}
```
-Javascript:
+### Javascript:
+
```javascript
/**
* @param {number[]} nums
@@ -449,7 +450,7 @@ var maxSlidingWindow = function (nums, k) {
};
```
-TypeScript:
+### TypeScript:
```typescript
function maxSlidingWindow(nums: number[], k: number): number[] {
@@ -497,7 +498,9 @@ function maxSlidingWindow(nums: number[], k: number): number[] {
};
```
-Swift:
+### Swift:
+
+解法一:
```Swift
/// 双向链表
@@ -638,7 +641,8 @@ func maxSlidingWindow(_ nums: [Int], _ k: Int) -> [Int] {
return result
}
```
-Scala:
+### Scala:
+
```scala
import scala.collection.mutable.ArrayBuffer
object Solution {
@@ -686,8 +690,8 @@ class MyQueue {
}
```
+### PHP:
-PHP:
```php
class Solution {
/**
@@ -764,7 +768,8 @@ class MyQueue{
}
```
-C#:
+### C#:
+
```csharp
class myDequeue{
private LinkedList linkedList = new LinkedList();
@@ -805,7 +810,7 @@ class myDequeue{
}
```
-rust:
+### Rust:
```rust
impl Solution {
diff --git a/problems/0242.有效的字母异位词.md b/problems/0242.有效的字母异位词.md
index 4ea43947..f47d8b05 100644
--- a/problems/0242.有效的字母异位词.md
+++ b/problems/0242.有效的字母异位词.md
@@ -7,7 +7,7 @@
> 数组就是简单的哈希表,但是数组的大小可不是无限开辟的
-## 242.有效的字母异位词
+# 242.有效的字母异位词
[力扣题目链接](https://leetcode.cn/problems/valid-anagram/)
@@ -21,13 +21,14 @@
输入: s = "rat", t = "car"
输出: false
-
**说明:**
你可以假设字符串只包含小写字母。
-## 思路
+## 算法公开课
-本题B站视频讲解版:[学透哈希表,数组使用有技巧!Leetcode:242.有效的字母异位词](https://www.bilibili.com/video/BV1YG411p7BA)
+**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[学透哈希表,数组使用有技巧!Leetcode:242.有效的字母异位词](https://www.bilibili.com/video/BV1YG411p7BA),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
+
+## 思路
先看暴力的解法,两层for循环,同时还要记录字符是否重复出现,很明显时间复杂度是 O(n^2)。
@@ -88,12 +89,10 @@ public:
* 时间复杂度: O(n)
* 空间复杂度: O(1)
-
-
## 其他语言版本
+### Java:
-Java:
```java
/**
* 242. 有效的字母异位词 字典解法
@@ -121,7 +120,7 @@ class Solution {
}
```
-Python:
+### Python:
```python
class Solution:
@@ -165,7 +164,7 @@ class Solution(object):
return a_count == b_count
```
-Go:
+### Go:
```go
func isAnagram(s string, t string) bool {
@@ -182,7 +181,7 @@ func isAnagram(s string, t string) bool {
}
```
-javaScript:
+### JavaScript:
```js
/**
@@ -218,7 +217,7 @@ var isAnagram = function(s, t) {
};
```
-TypeScript:
+### TypeScript:
```typescript
function isAnagram(s: string, t: string): boolean {
@@ -233,7 +232,7 @@ function isAnagram(s: string, t: string): boolean {
};
```
-Swift:
+### Swift:
```Swift
func isAnagram(_ s: String, _ t: String) -> Bool {
@@ -257,7 +256,8 @@ func isAnagram(_ s: String, _ t: String) -> Bool {
}
```
-PHP:
+### PHP:
+
```php
class Solution {
/**
@@ -292,7 +292,8 @@ class Solution {
}
```
-Rust:
+### Rust:
+
```rust
impl Solution {
pub fn is_anagram(s: String, t: String) -> bool {
@@ -312,8 +313,8 @@ impl Solution {
}
```
+### Scala:
-Scala:
```scala
object Solution {
def isAnagram(s: String, t: String): Boolean = {
@@ -337,8 +338,8 @@ object Solution {
}
```
+### C#:
-C#:
```csharp
public bool IsAnagram(string s, string t) {
int sl=s.Length,tl=t.Length;
@@ -360,11 +361,12 @@ C#:
## 相关题目
* [383.赎金信](https://programmercarl.com/0383.%E8%B5%8E%E9%87%91%E4%BF%A1.html)
-* 49.字母异位词分组
-* 438.找到字符串中所有字母异位词
+* [49.字母异位词分组](https://leetcode.cn/problems/group-anagrams/)
+* [438.找到字符串中所有字母异位词](https://leetcode.cn/problems/find-all-anagrams-in-a-string/)
+
diff --git a/problems/0257.二叉树的所有路径.md b/problems/0257.二叉树的所有路径.md
index 06153507..44c0fd85 100644
--- a/problems/0257.二叉树的所有路径.md
+++ b/problems/0257.二叉树的所有路径.md
@@ -18,9 +18,11 @@
示例:

-# 思路
+## 算法公开课
-**《代码随想录》算法视频公开课:[递归中带着回溯,你感受到了没?| LeetCode:257. 二叉树的所有路径](https://www.bilibili.com/video/BV1ZG411G7Dh),相信结合视频在看本篇题解,更有助于大家对本题的理解**。
+**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html)::[递归中带着回溯,你感受到了没?| LeetCode:257. 二叉树的所有路径](https://www.bilibili.com/video/BV1ZG411G7Dh),相信结合视频在看本篇题解,更有助于大家对本题的理解**。
+
+## 思路
这道题目要求从根节点到叶子的路径,所以需要前序遍历,这样才方便让父节点指向孩子节点,找到对应的路径。
@@ -32,7 +34,7 @@
我们先使用递归的方式,来做前序遍历。**要知道递归和回溯就是一家的,本题也需要回溯。**
-## 递归
+### 递归
1. 递归函数参数以及返回值
@@ -305,7 +307,7 @@ public:
**综合以上,第二种递归的代码虽然精简但把很多重要的点隐藏在了代码细节里,第一种递归写法虽然代码多一些,但是把每一个逻辑处理都完整的展现出来了。**
-## 拓展
+### 拓展
这里讲解本题解的写法逻辑以及一些更具体的细节,下面的讲解中,涉及到C++语法特性,如果不是C++的录友,就可以不看了,避免越看越晕。
@@ -328,7 +330,7 @@ public:
所以,第一个代码版本中,我才使用 vector 类型的path,这样方便给大家演示代码中回溯的操作。 vector类型的path,不管 每次 路径收集的数字是几位数,总之一定是int,所以就一次 pop_back就可以。
-## 迭代法
+### 迭代法
至于非递归的方式,我们可以依然可以使用前序遍历的迭代方式来模拟遍历路径的过程,对该迭代方式不了解的同学,可以看文章[二叉树:听说递归能做的,栈也能做!](https://programmercarl.com/二叉树的迭代遍历.html)和[二叉树:前中后序迭代方式统一写法](https://programmercarl.com/二叉树的统一迭代法.html)。
@@ -368,7 +370,7 @@ public:
```
当然,使用java的同学,可以直接定义一个成员变量为object的栈`Stack