最初的最初
一开始打算这样写的。但是呢,人家题目的要求是,必须到达叶子节点的一个路径。
这个的思路是,最后到叶子下面的null时候再去做判断这个路径是否是符合要求的。
里面有很多傻逼代码,轻喷。
// 这样写code会被写进去两次,因为叶子节点下面左右两个还会有指针,而path并没有变化。除非你拿map记录是否重复,才能有正确答案。
// 而且如果path这个变量写在函数里面(作为参数传递),其实是和path放在外面的效果是一样的,因为所有变量都是引用。
class Solution {
public LinkedList<List<Integer>> res;
public LinkedList<Integer> path=new LinkedList<Integer>();
public HashMap<LinkedList,Integer> vis;
public int sum(LinkedList<Integer> tmp){
int sum_res=0;
for (int num:tmp)
sum_res+=num;
return sum_res;
}
public void getRes(TreeNode root,int target){
if(root==null){
if(target==sum(path)){
if(vis.containsKey(path)) return;
vis.put(path,1);
LinkedList<Integer> tmp=new LinkedList<Integer>(path);
Collections.reverse(tmp);
res.push(new LinkedList<Integer>(tmp));
}
return;
}
path.push(root.val);
// int code=System.identityHashCode(path);
// System.out.println(code);
getRes(root.left,target);
getRes(root.right,target);
path.pop();
return;
}
public List<List<Integer>> pathSum(TreeNode root, int target) {
res=new LinkedList<>();
if(root==null) return res;
LinkedList<Integer> path =new LinkedList<Integer>();
vis=new HashMap<LinkedList,Integer>();
getRes(root,target);
Collections.reverse(res);
return res;
}
}
这个的反例就是[1,2],1 这个样例,由于1节点右侧没有节点,所以会被判别一次,而这次的path的和刚好是1,于是会误判。
- 所以这个不能递归到叶子下的null指针时候判别,而应该在节点左右孩子都无的时候判断。
- 而且这样sum符合的path会被写进去两次,因为叶子节点下面左右两个还会有指针,而path并没有变化。除非你拿map记录是否重复,才能有正确答案。
- 由于我这里用了push和pop,又用了递归,所以得到的答案顺序是相反的,应当用add和removeLast。
改一下
这里是把1和2点都改了,因为不是用null的时候去判断了,那么path的重复问题自然就没了。
ac:
// java, all of the variables are all reference! REMEMBER!
class Solution {
public LinkedList<List<Integer>> res;
public LinkedList<Integer> path=new LinkedList<Integer>();
public int sum(LinkedList<Integer> tmp){
int sum_res=0;
for (int num:tmp)
sum_res+=num;
return sum_res;
}
public void getRes(TreeNode root,int target){
if(root==null) return;
path.push(root.val);
if(target==sum(path)&&root.right==null && root.left==null){
System.out.println(sum(path));
LinkedList<Integer> tmp=new LinkedList<Integer>(path);
Collections.reverse(tmp);
res.push(new LinkedList<Integer>(tmp));
}
getRes(root.left,target);
getRes(root.right,target);
path.pop();
return;
}
public List<List<Integer>> pathSum(TreeNode root, int target) {
res=new LinkedList<>();
if(root==null) return res;
LinkedList<Integer> path =new LinkedList<Integer>();
getRes(root,target);
Collections.reverse(res);
return res;
}
}
继续改
显然上面ac的代码显得我像个傻逼。还要重新collections.reverse。应当用add和removeLast。
class Solution {
public LinkedList<List<Integer>> res=new LinkedList<>();
public LinkedList<Integer> path=new LinkedList<Integer>();
public int sum(LinkedList<Integer> tmp){
int sum_res=0;
for (int num:tmp)
sum_res+=num;
return sum_res;
}
public void getRes(TreeNode root,int target){
if(root==null) return;
path.add(root.val);
System.out.println(path);
if(root.right==null && root.left==null && target==sum(path)){
res.add(new LinkedList<Integer>(path));
}
getRes(root.left,target);
getRes(root.right,target);
path.removeLast();
return;
}
public List<List<Integer>> pathSum(TreeNode root, int target) {
if(root==null) return res;
getRes(root,target);
return res;
}
}
成绩是:3 ms 38.8 MB,比之前的13 ms 38.8 MB快了不少。
还想再省快一点
显然对于判断的那个target变量,我是自己谢了个sum去判断path的和,但是duck不必这么做,可以在每一层的时候用target去减就行了,到最后看target是否等于0就可以。
可以省掉sum这个函数的空间。这个改起来其实很简单,就把target和判别条件改改就行了
class Solution {
public LinkedList<List<Integer>> res=new LinkedList<>();
public LinkedList<Integer> path=new LinkedList<Integer>();
public void getRes(TreeNode root,int target){
if(root==null) return;
path.add(root.val);
// System.out.println(path);
target-=root.val;
if(root.right==null && root.left==null && target==0){
res.add(new LinkedList<Integer>(path));
}
getRes(root.left,target);
getRes(root.right,target);
path.removeLast();
return;
}
public List<List<Integer>> pathSum(TreeNode root, int target) {
if(root==null) return res;
getRes(root,target);
return res;
}
}
1 ms 38.7 MB
还行吧。
==================================================
以下和本题无关,不必看:
// java, all of the variables are all reference! REMEMBER!