剑指offer——二叉树中和为某一值的路径(不错)

面25
题目描述
输入一颗二叉树和一个整数,打印出二叉树中结点值的和为输入整数的所有路径。路径定义为从树的根结点开始往下一直到叶结点所经过的结点形成一条路径。

思路:
【审题错误!】不知道节点的值是否有负数的情况,如果可以为负,那么即使和已经为target了,也要再继续。

【看清楚后】发现路径的定义是到叶节点才能称为路径。所以情况变简单了。

错误代码:

第二个方法中的list始终是同一个,所以即使在满足条件的时候放进result里,到最后result里的几个元素都指向同一个list

    ArrayList<ArrayList<Integer>> result = new ArrayList<>();
    public ArrayList<ArrayList<Integer>> FindPath(TreeNode root,int target) {
        ArrayList<Integer> list = new ArrayList<>();
        if(root==null)
            return result;
        else
            helper(root,target,0,list);
        return result;
    }

    public ArrayList<Integer> helper(TreeNode root, int target, int sum ,ArrayList<Integer> list){
        if(root == null)
            return null;
        sum = sum+root.val;
        list.add(root.val);
        if(sum==target)
            result.add(list);
        helper(root.left,target,sum,list);
        helper(root.right,target,sum,list);
        return null;
    }
}

【错误审题后的答案】可以得到所有可能的解。节点值为负数也适用。(即中途走到就停)

ArrayList<ArrayList<Integer>> result = new ArrayList<>();
    public ArrayList<ArrayList<Integer>> FindPath(TreeNode root,int target) {
        ArrayList<Integer> list = new ArrayList<>();
        if(root==null)
            return result;
        else
            helper(root,target,0,list);
        return result;
    }

    public ArrayList<Integer> helper(TreeNode root, int target, int sum ,ArrayList<Integer> list){
        if(root == null)
            return null;
        sum = sum+root.val;
        ArrayList<Integer> now = new ArrayList<>(list); // 这个方法,每次都生成新的list,避免了存入result里的list还会动态变化的情况。 注意这里的构造方法。
        now.add(root.val);
        if(sum==target)
            result.add(now);
        helper(root.left,target,sum,now);
        helper(root.right,target,sum,now);
        return null;
    }

正确审题后

public ArrayList<Integer> helper(TreeNode root, int target, int sum ,ArrayList<Integer> list){
        if(root==null)
            return null;
         ArrayList<Integer> now = new ArrayList<>(list); // 但这样空间占用会很多,生成了很多对象
        now.add(root.val);
        sum = sum+root.val;
        if(root.left == null&&root.right==null){
            if(sum==target) {
                result.add(now);
            }
            return null;
        }
        helper(root.left,target,sum,now);
        helper(root.right,target,sum,now);
        return null;
    }

比较精简的写法(不需要额外辅助函数,注意返回值):

public class Solution {
    private ArrayList<ArrayList<Integer>> listAll = new ArrayList<ArrayList<Integer>>();
    private ArrayList<Integer> list = new ArrayList<Integer>();
    public ArrayList<ArrayList<Integer>> FindPath(TreeNode root,int target) {
        if(root == null) return listAll;
        list.add(root.val);
        target -= root.val;
        if(target == 0 && root.left == null && root.right == null) 
            listAll.add(new ArrayList<Integer>(list)); // listAll.add((ArrayList<Integer>)list.clone());
        FindPath(root.left, target);
        FindPath(root.right, target);
        list.remove(list.size()-1);
        //会返回给调用它的函数。所以只有main函数调用的FindPath()会直接返回给主函数  
        return listAll;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值