更新 0112.路径总和 排版格式修复

This commit is contained in:
jinbudaily
2023-07-21 17:53:29 +08:00
parent f3a3701130
commit bf6c4e7f7b

View File

@ -21,9 +21,9 @@
返回 true, 因为存在目标和为 22 的根节点到叶子节点的路径 5->4->11->2。 返回 true, 因为存在目标和为 22 的根节点到叶子节点的路径 5->4->11->2。
## 视频讲解 ## 算法公开课
**《代码随想录》算法视频公开课:[拿不准的遍历顺序,搞不清的回溯过程,我太难了! | LeetCode112. 路径总和](https://www.bilibili.com/video/BV19t4y1L7CR),相信结合视频在看本篇题解,更有助于大家对本题的理解**。 **[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html)[拿不准的遍历顺序,搞不清的回溯过程,我太难了! | LeetCode112. 路径总和](https://www.bilibili.com/video/BV19t4y1L7CR),相信结合视频在看本篇题解,更有助于大家对本题的理解**。
## 思路 ## 思路
@ -32,8 +32,8 @@
那么接下来我通过详细讲解如下两道题,来回答这个问题: 那么接下来我通过详细讲解如下两道题,来回答这个问题:
* 112.路径总和 * [112.路径总和](https://leetcode.cn/problems/path-sum/)
* 113.路径总和ii * [113.路径总和ii](https://leetcode.cn/problems/path-sum-ii/)
这道题我们要遍历从根节点到叶子节点的路径看看总和是不是目标和。 这道题我们要遍历从根节点到叶子节点的路径看看总和是不是目标和。
@ -218,7 +218,9 @@ public:
如果大家完全理解了本题的递归方法之后就可以顺便把leetcode上113. 路径总和ii做了。 如果大家完全理解了本题的递归方法之后就可以顺便把leetcode上113. 路径总和ii做了。
# 113. 路径总和ii ## 相关题目推荐
### 113. 路径总和ii
[力扣题目链接](https://leetcode.cn/problems/path-sum-ii/) [力扣题目链接](https://leetcode.cn/problems/path-sum-ii/)
@ -232,7 +234,7 @@ public:
![113.路径总和ii1.png](https://code-thinking-1253855093.file.myqcloud.com/pics/20210203160854654.png) ![113.路径总和ii1.png](https://code-thinking-1253855093.file.myqcloud.com/pics/20210203160854654.png)
## 思路 ### 思路
113.路径总和ii要遍历整个树找到所有路径**所以递归函数不要返回值!** 113.路径总和ii要遍历整个树找到所有路径**所以递归函数不要返回值!**
@ -289,7 +291,7 @@ public:
至于113. 路径总和ii 的迭代法我并没有写,用迭代方式记录所有路径比较麻烦,也没有必要,如果大家感兴趣的话,可以再深入研究研究。 至于113. 路径总和ii 的迭代法我并没有写,用迭代方式记录所有路径比较麻烦,也没有必要,如果大家感兴趣的话,可以再深入研究研究。
## 总结 ### 总结
本篇通过leetcode上112. 路径总和 和 113. 路径总和ii 详细的讲解了 递归函数什么时候需要返回值,什么不需要返回值。 本篇通过leetcode上112. 路径总和 和 113. 路径总和ii 详细的讲解了 递归函数什么时候需要返回值,什么不需要返回值。
@ -300,11 +302,11 @@ public:
# 其他语言版本 ## 其他语言版本
## java ### Java
### 0112.路径总和 #### 0112.路径总和
```java ```java
class solution { class solution {
@ -422,7 +424,7 @@ class solution {
} }
``` ```
### 0113.路径总和-ii #### 0113.路径总和-ii
```java ```java
class solution { class solution {
@ -529,9 +531,9 @@ class Solution {
} }
``` ```
## python ### Python
### 0112.路径总和 #### 0112.路径总和
(版本一) 递归 (版本一) 递归
```python ```python
@ -618,7 +620,7 @@ class Solution:
### 0113.路径总和-ii #### 0113.路径总和-ii
(版本一) 递归 (版本一) 递归
```python ```python
@ -719,9 +721,9 @@ class Solution:
``` ```
## go ### Go
### 112. 路径总和 #### 112. 路径总和
```go ```go
//递归法 //递归法
@ -746,7 +748,7 @@ func hasPathSum(root *TreeNode, targetSum int) bool {
} }
``` ```
### 113. 路径总和 II #### 113. 路径总和 II
```go ```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,10 @@ func traversal(_ cur: TreeNode?, count: Int) {
} }
``` ```
## C ### C
> 0112.路径总和 #### 0112.路径总和
> 递归法: 递归法:
```c ```c
bool hasPathSum(struct TreeNode* root, int targetSum){ bool hasPathSum(struct TreeNode* root, int targetSum){
@ -1252,7 +1254,7 @@ bool hasPathSum(struct TreeNode* root, int targetSum){
} }
``` ```
> 0113.路径总和 II #### 0113.路径总和 II
```c ```c
int** ret; int** ret;
@ -1317,9 +1319,9 @@ int** pathSum(struct TreeNode* root, int targetSum, int* returnSize, int** retur
} }
``` ```
## Scala ### Scala
### 0112.路径总和 #### 0112.路径总和
**递归:** **递归:**
@ -1369,7 +1371,7 @@ object Solution {
} }
``` ```
### 0113.路径总和 II #### 0113.路径总和 II
**递归:** **递归:**
@ -1405,9 +1407,9 @@ object Solution {
} }
``` ```
## rust ### Rust
### 112.路径总和.md #### 0112.路径总和
递归: 递归:
@ -1461,7 +1463,7 @@ impl Solution {
} }
``` ```
### 113.路径总和-ii #### 0113.路径总和-ii
```rust ```rust
impl Solution { impl Solution {
@ -1514,3 +1516,4 @@ impl Solution {
<a href="https://programmercarl.com/other/kstar.html" target="_blank"> <a href="https://programmercarl.com/other/kstar.html" target="_blank">
<img src="../pics/网站星球宣传海报.jpg" width="1000"/> <img src="../pics/网站星球宣传海报.jpg" width="1000"/>
</a> </a>