Merge pull request #1289 from cy948/patch-2

Update 0112.路径总和.md
This commit is contained in:
程序员Carl
2022-05-25 09:47:09 +08:00
committed by GitHub

View File

@ -377,22 +377,22 @@ class solution {
```java ```java
class solution { class solution {
public list<list<integer>> pathsum(treenode root, int targetsum) { public List<List<Integer>> pathsum(TreeNode root, int targetsum) {
list<list<integer>> res = new arraylist<>(); List<List<Integer>> res = new ArrayList<>();
if (root == null) return res; // 非空判断 if (root == null) return res; // 非空判断
list<integer> path = new linkedlist<>(); List<Integer> path = new LinkedList<>();
preorderdfs(root, targetsum, res, path); preorderdfs(root, targetsum, res, path);
return res; return res;
} }
public void preorderdfs(treenode root, int targetsum, list<list<integer>> res, list<integer> path) { public void preorderdfs(TreeNode root, int targetsum, List<List<Integer>> res, List<Integer> path) {
path.add(root.val); path.add(root.val);
// 遇到了叶子节点 // 遇到了叶子节点
if (root.left == null && root.right == null) { if (root.left == null && root.right == null) {
// 找到了和为 targetsum 的路径 // 找到了和为 targetsum 的路径
if (targetsum - root.val == 0) { if (targetsum - root.val == 0) {
res.add(new arraylist<>(path)); res.add(new ArrayList<>(path));
} }
return; // 如果和不为 targetsum返回 return; // 如果和不为 targetsum返回
} }