mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-10 04:06:51 +08:00
添加0112.路径总和.mdJava解法
This commit is contained in:
@ -411,6 +411,31 @@ class solution {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
```java
|
||||||
|
// 解法2
|
||||||
|
class Solution {
|
||||||
|
List<List<Integer>> result;
|
||||||
|
LinkedList<Integer> path;
|
||||||
|
public List<List<Integer>> pathSum (TreeNode root,int targetSum) {
|
||||||
|
result = new LinkedList<>();
|
||||||
|
path = new LinkedList<>();
|
||||||
|
travesal(root, targetSum);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
private void travesal(TreeNode root, int count) {
|
||||||
|
if (root == null) return;
|
||||||
|
path.offer(root.val);
|
||||||
|
count -= root.val;
|
||||||
|
if (root.left == null && root.right == null && count == 0) {
|
||||||
|
result.add(new LinkedList<>(path));
|
||||||
|
}
|
||||||
|
travesal(root.left, count);
|
||||||
|
travesal(root.right, count);
|
||||||
|
path.removeLast(); // 回溯
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
## python
|
## python
|
||||||
|
|
||||||
0112.路径总和
|
0112.路径总和
|
||||||
|
Reference in New Issue
Block a user