Skip to content

Commit

Permalink
Merge pull request youngyangyang04#1289 from cy948/patch-2
Browse files Browse the repository at this point in the history
Update 0112.路径总和.md
  • Loading branch information
youngyangyang04 committed May 25, 2022
2 parents cacbbea + cb6f015 commit f7ca72c
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions problems/0112.路径总和.md
Original file line number Diff line number Diff line change
Expand Up @@ -377,22 +377,22 @@ class solution {

```java
class solution {
public list<list<integer>> pathsum(treenode root, int targetsum) {
list<list<integer>> res = new arraylist<>();
public List<List<Integer>> pathsum(TreeNode root, int targetsum) {
List<List<Integer>> res = new ArrayList<>();
if (root == null) return res; // 非空判断
list<integer> path = new linkedlist<>();

List<Integer> path = new LinkedList<>();
preorderdfs(root, targetsum, res, path);
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);
// 遇到了叶子节点
if (root.left == null && root.right == null) {
// 找到了和为 targetsum 的路径
if (targetsum - root.val == 0) {
res.add(new arraylist<>(path));
res.add(new ArrayList<>(path));
}
return; // 如果和不为 targetsum,返回
}
Expand Down

0 comments on commit f7ca72c

Please sign in to comment.