forked from JadeZYX/Java_LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathP0113pathSum2BT.java
More file actions
68 lines (62 loc) · 2.51 KB
/
Copy pathP0113pathSum2BT.java
File metadata and controls
68 lines (62 loc) · 2.51 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
import java.util.ArrayList;
import java.util.List;
public class P0113pathSum2BT {
List<List<Integer>>ans=new ArrayList<>();
int Tsum;
public List<List<Integer>>pathSum(TreeNode root,int targetSum){//加法 backtracking
this.Tsum=targetSum;
List<Integer>paths=new ArrayList<>();
helpPath(root,0,paths);
return ans;
}
void helpPath(TreeNode node, int sum, List<Integer>path){
if(node==null) return;
sum+=node.val;
path.add(node.val);
if(node.left==null&&node.right==null){
if(sum==Tsum){
ans.add(new ArrayList<>(path));//copy出来的list,对原结果没有影响
}
}
helpPath(node.left, sum,path);
helpPath(node.right, sum,path);
path.remove(path.size()-1);//返回之前要把刚添加的节点值给删除
//比如传入节点4后,也调用了4的left subtree和right subtree后就可以删除节点4,这样path里面就只剩下节点5,继续5的right subtree的遍历
}
public List<List<Integer>>pathSum1(TreeNode root,int targetSum){//减法
List<List<Integer>>list = new ArrayList<>();
if(root == null) return list;
pathSumHelper(root,targetSum,new ArrayList<Integer>(),list);
return list;
}
private void pathSumHelper(TreeNode node, int target, List<Integer>path,List<List<Integer>>paths){
path.add(node.val);//因为已经判定过传入的Node不为NULL
if(node.left ==null && node.right == null && node.val == target){
paths.add(new ArrayList<>(path));
}
if(node.left != null){
pathSumHelper(node.left, target-node.val, path, paths);
}
if(node.right != null){
pathSumHelper(node.right, target-node.val, path, paths);
}
path.remove(path.size()-1);
}
public List<List<Integer>> pathSum2(TreeNode root, int targetSum) {
List<List<Integer>>list = new ArrayList<>();
if(root == null) return list;
helper(root, targetSum, list, new ArrayList<>());
return list;
}
void helper(TreeNode node, int target,List<List<Integer>>list,ArrayList<Integer>path){
if(node == null) return;
path.add(node.val);
target-=node.val;
if(node.left == null && node.right == null && target ==0){
list.add(new ArrayList<>(path));
}
helper(node.left,target,list,path);
helper(node.right,target,list,path);
path.remove(path.size()-1);
}
}