-
Notifications
You must be signed in to change notification settings - Fork 266
Expand file tree
/
Copy path34.java
More file actions
56 lines (31 loc) · 850 Bytes
/
34.java
File metadata and controls
56 lines (31 loc) · 850 Bytes
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
class Solution {
List<List<Integer>> result = new LinkedList<List<Integer>>();
public List<List<Integer>> pathSum(TreeNode root, int sum) {
if(root==null)
{
return result;
}
List<Integer> list = new LinkedList<Integer>();
find(list,root,0,sum);
return result;
}
public void find(List<Integer> list,TreeNode root,int target,int sum)
{
if(root==null)
{
return;
}
target+=root.val;
list.add(root.val);
if(target==sum&&root.left==null&&root.right==null)
{
result.add(new LinkedList<>(list));
}
else
{
find(list,root.left,target,sum);
find(list,root.right,target,sum);
}
list.remove(list.size()-1);
}
}