-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathPathSumII.java
77 lines (65 loc) · 1.96 KB
/
PathSumII.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
package leetcode.solution.backtrack;
import leetcode.structure.TreeNode;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
/**
* 113. Path Sum II
*/
public class PathSumII {
public static void main(String[] args) {
Integer[] tree = {5, 4, 8, 11, null, 13, 4, 7, 2, null, null, 5, 1};
TreeNode treeNode = TreeNode.constructTree(tree);
int sum = 22;
PathSumII palindromePartitioning = new PathSumII();
List<List<Integer>> ans = palindromePartitioning.pathSum(treeNode, sum);
System.out.println(ans);
// [[5,4,11,2],[5,8,4,5]]
}
/**
* global variable - result list
*/
List<List<Integer>> ans;
/**
* global variable - targetSum
*/
int target;
public List<List<Integer>> pathSum(TreeNode root, int targetSum) {
ans = new ArrayList<>();
if (root == null) {
return ans;
}
target = targetSum;
LinkedList<Integer> path = new LinkedList<>();
// root value will always be chosen
path.add(root.val);
int sum = root.val;
backtrack(root, sum, path);
return ans;
}
private void backtrack(TreeNode root, int sum, LinkedList<Integer> path) {
// arrive the leaf node
if (root.left == null && root.right == null) {
if (sum == target) {
ans.add(new ArrayList<>(path));
}
return;
}
// search left child
if (root.left != null) {
path.add(root.left.val);
sum += root.left.val;
backtrack(root.left, sum, path);
path.removeLast();
sum -= root.left.val;
}
// search right child
if (root.right != null) {
path.add(root.right.val);
sum += root.right.val;
backtrack(root.right, sum, path);
path.removeLast();
sum -= root.right.val;
}
}
}