剑指 Offer 34. 二叉树中和为某一值的路径java

最初的最初

一开始打算这样写的。但是呢,人家题目的要求是,必须到达叶子节点的一个路径。
这个的思路是,最后到叶子下面的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,于是会误判。

  1. 所以这个不能递归到叶子下的null指针时候判别,而应该在节点左右孩子都无的时候判断。
  2. 而且这样sum符合的path会被写进去两次,因为叶子节点下面左右两个还会有指针,而path并没有变化。除非你拿map记录是否重复,才能有正确答案。
  3. 由于我这里用了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!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值