-
-
Notifications
You must be signed in to change notification settings - Fork 300
/
113.java
82 lines (75 loc) · 2.56 KB
/
113.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
__________________________________________________________________________________________________
sample 1 ms submission
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public List<List<Integer>> pathSum(TreeNode root, int sum) {
List<List<Integer>> list = new LinkedList<List<Integer>>();
if(root == null){
return list;
}
List<Integer> list2 = new LinkedList<Integer>();
pathSum(root, sum, 0, list, list2);
return list;
}
public void pathSum(TreeNode root, int sum, int temp, List<List<Integer>> list, List<Integer> list2){
if(root.left == null && root.right == null && temp+root.val == sum){
list2.add(root.val);
list.add(new LinkedList(list2));
list2.remove(list2.size()-1);
}
if(root.left != null){
list2.add(root.val);
pathSum(root.left,sum,temp+root.val, list, list2);
list2.remove(list2.size()-1);
}
if(root.right != null){
list2.add(root.val);
pathSum(root.right,sum,temp+root.val, list, list2);
list2.remove(list2.size()-1);
}
}
}
__________________________________________________________________________________________________
sample 34972 kb submission
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public List<List<Integer>> pathSum(TreeNode root, int sum) {
LinkedList<List<Integer>> sol = new LinkedList<List<Integer>>();
solve(root, sum, sol, 0, new LinkedList<Integer>());
return (sol);
}
private void solve (TreeNode root, int target, LinkedList<List<Integer>> sol, int sum, LinkedList<Integer> partial) {
if (root == null) {
return;
}
sum += root.val;
partial.add(root.val);
if (root.left == null && root.right == null) {
if (sum == target) {
sol.add(new LinkedList<Integer>(partial));
}
partial.removeLast();
return;
}
solve(root.left, target, sol, sum, partial);
solve(root.right, target, sol, sum, partial);
partial.removeLast();
}
}
__________________________________________________________________________________________________