求路径和,请问我这错在哪儿

public ArrayList<ArrayList<Integer>> FindPath(TreeNode root,int target) {
        ArrayList<ArrayList<Integer>> paths = new ArrayList<ArrayList<Integer>>();
        if(root == null)return paths;
        ArrayList<Integer> path = new ArrayList<Integer>();
        int currentSum = 0;
        FindPath(root,target,path,paths,currentSum);
        return paths;
        
        
    }
    public void FindPath(TreeNode root,int target,ArrayList<Integer> path,ArrayList<ArrayList<Integer>> paths,int currentNum) {
        currentNum += root.val;
        path.add(root.val);
        boolean isleaf = ((root.left == null)&&(root.right == null));
        if((currentNum == target) && isleaf) {
            paths.add(path);
        }
        if(root.left != null)
            FindPath(root.left,target,path,paths,currentNum);
        if(root.right != null)
            FindPath(root.right,target,path,paths,currentNum);
        path.remove(path.size() - 1);
    }
结果显示为空,请问为什么
全部评论
if((currentNum == target) && isleaf) {              paths.add(path);          } 改为: if((currentNum == target) && isleaf) {              ArrayList<Integer> path1=new  ArrayList<Integer>();             path1.addAll(path)             paths.add(path1);          }
点赞 回复 分享
发布于 2017-03-30 20:15

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务