-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path113-PathSumII.java
More file actions
28 lines (23 loc) · 984 Bytes
/
Copy path113-PathSumII.java
File metadata and controls
28 lines (23 loc) · 984 Bytes
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
class Solution {
public List<List<Integer>> pathSum(TreeNode root, int targetSum) {
List<List<Integer>> result = new ArrayList<>();
backtrack(root, targetSum, result, new ArrayList<>());
return result;
}
private void backtrack(TreeNode root, int targetSum, List<List<Integer>> result, List<Integer> temp) {
if (root == null)
return;
temp.add(root.val);
// add the copy of the temp list(not temp itself) when adding it to result.
// targetSum = root.val(root 가 leaf 일떄 그 root.val(마지막 root))
if (isLeaf(root) && root.val == targetSum) {
result.add(new ArrayList<>(temp));
}
backtrack(root.left, targetSum - root.val, result, temp);
backtrack(root.right, targetSum - root.val, result, temp);
temp.remove(temp.size() - 1);
}
private boolean isLeaf(TreeNode root) {
return root.left == null && root.right == null;
}
}