-
Notifications
You must be signed in to change notification settings - Fork 34
/
Solution.java
54 lines (54 loc) · 1.84 KB
/
Solution.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
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public List<List<Integer>> pathSum(TreeNode root, int sum) {
List<List<Integer>> answerList=new ArrayList<List<Integer>>();
if (root==null) {
return answerList;
}
List<List<TreeNode>> rootList=new ArrayList<List<TreeNode>>();
List<TreeNode> initList=new ArrayList<TreeNode>();
initList.add(root);
rootList.add(initList);
for (int i = 0; i < rootList.size(); i++) {
List<TreeNode> list=rootList.get(i);
TreeNode node=list.get(list.size()-1);
if (node.left!=null||node.right!=null) {
if (node.left!=null) {
List<TreeNode> newLeftList=new ArrayList<TreeNode>();
newLeftList.addAll(list);
newLeftList.add(node.left);
rootList.add(newLeftList);
}
if (node.right!=null) {
List<TreeNode> newRightList=new ArrayList<TreeNode>();
newRightList.addAll(list);
newRightList.add(node.right);
rootList.add(newRightList);
}
rootList.remove(i);
i--;
}
}
for (int i = 0; i < rootList.size(); i++) {
List<TreeNode> list=rootList.get(i);
List<Integer> valList=new ArrayList<Integer>();
int val=0;
for (int j = 0; j < list.size(); j++) {
val+=list.get(j).val;
valList.add(list.get(j).val);
}
if (val==sum) {
answerList.add(valList);
}
}
return answerList;
}
}