-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathPathSumII.java
More file actions
72 lines (64 loc) · 1.67 KB
/
PathSumII.java
File metadata and controls
72 lines (64 loc) · 1.67 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
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
package algorithm.lc;
import java.util.ArrayList;
/**
* Given a binary tree and a sum, find all root-to-leaf paths where each path's
* sum equals the given sum.
*
* For example: Given the below binary tree and sum = 22,
* 5
* / \
* 4 8
* / / \
* 11 13 4
* / \ / \
* 7 2 5 1
*
* return
*
* [ [5,4,11,2], [5,8,4,5] ]
*
*/
public class PathSumII {
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) {
val = x;
}
}
// O(nlgn) space, O(n) time
public class Solution {
public ArrayList<ArrayList<Integer>> pathSum(TreeNode root, int sum) {
// Start typing your Java solution below
// DO NOT write main() function
ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>>();
if (root == null) {
return res;
}
ArrayList<Integer> cur = new ArrayList<Integer>();
int curSum = 0;
getSum(root, sum, cur, curSum, res);
return res;
}
private void getSum(TreeNode node, int sum, ArrayList<Integer> cur,
int curSum, ArrayList<ArrayList<Integer>> res) {
if (node.left == null && node.right == null) {
if (curSum + node.val == sum) {
ArrayList<Integer> copy = new ArrayList<Integer>(cur);
copy.add(node.val);
res.add(copy);
}
} else {
cur.add(node.val);
if (node.left != null) {
getSum(node.left, sum, cur, curSum + node.val, res);
}
if (node.right != null) {
getSum(node.right, sum, cur, curSum + node.val, res);
}
cur.remove(cur.size() - 1);
}
}
}
}