添加0112.路径总和.mdJava解法

This commit is contained in:
ironartisan
2021-09-06 10:50:21 +08:00
parent 429b504779
commit cae6b56b2d

View File

@ -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
0112.路径总和