-
Notifications
You must be signed in to change notification settings - Fork 0
/
PathSumII.java
38 lines (34 loc) · 1.21 KB
/
PathSumII.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
/* 典型的树的深度搜索,如何添加path进入res?
当我们发现一条路的时候,将path加入res,然后需要移除path当中的最后一个Node */
public class Solution {
public List<List<Integer>> pathSum(TreeNode root, int sum) {
List<List<Integer>> res = new ArrayList<List<Integer>>();
ArrayList<Integer> path = new ArrayList<Integer>();
pathDFS(root, sum, path, res);
return res;
}
private void pathDFS(TreeNode root, int sum, ArrayList<Integer> path, List<List<Integer>> res){
if(root == null) return;
if(root.left == null && root.right == null){
if(sum == root.val){
path.add(root.val);
res.add(new ArrayList<Integer>(path));
path.remove(path.size()-1);
}
return;
}
path.add(root.val);
pathDFS(root.left, sum - root.val, path, res);
pathDFS(root.right, sum - root.val, path, res);
path.remove(path.size()-1);
}
}