113. Path Sum II

class Solution {
    public List<List<Integer>> pathSum(TreeNode root, int sum) {
        List<List<Integer>> res=new ArrayList<List<Integer>>();
        pathSum(root, sum, new ArrayList<Integer>(), res);
        return res;
    }
    private void pathSum(TreeNode node, int sum, List<Integer> list, List<List<Integer>> res){
        if(node==null)
            return;
        list.add(node.val);
        if(node.left==null&&node.right==null)
        {
            if(node.val==sum)
                res.add(new ArrayList<Integer>(list));
        }
        else
        {
            if(node.left!=null)
                pathSum(node.left, sum-node.val, list, res);
            if(node.right!=null)
                pathSum(node.right, sum-node.val, list, res);
        }
        list.remove(list.size()-1);
    }
}

  

posted @ 2017-09-30 02:22  Weiyu Wang  阅读(113)  评论(0编辑  收藏  举报