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

来源:互联网 发布:win7系统禁止安装软件 编辑:程序博客网 时间:2024/06/14 18:36

面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;    }}