-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path113.path-sum-ii.java
More file actions
35 lines (34 loc) · 1.23 KB
/
113.path-sum-ii.java
File metadata and controls
35 lines (34 loc) · 1.23 KB
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
/**
* 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>> result = new ArrayList<>();
List<Integer> resulti = new ArrayList<>();
pathSum2(result,resulti,root,sum);
return result;
}
public void pathSum2(List<List<Integer>> result, List<Integer> resulti, TreeNode root, int sum){
if (root==null) return;
else {
if (sum==root.val&&root.left==null&&root.right==null) {
List<Integer> resulticpy = new ArrayList<>(resulti);
resulticpy.add(root.val);
result.add(resulticpy);
} else {
List<Integer> resulticpy1 = new ArrayList<>(resulti);
List<Integer> resulticpy2 = new ArrayList<>(resulti);
resulticpy1.add(root.val);
resulticpy2.add(root.val);
pathSum2(result,resulticpy1,root.left,sum-root.val);
pathSum2(result,resulticpy1,root.right,sum-root.val);
}
}
}
}