See More

/** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ public class Solution { public ArrayList> pathSum(TreeNode root, int sum) { // Start typing your Java solution below // DO NOT write main() function if(root==null) return new ArrayList>(); if(root.left==null && root.right==null && sum == root.val) { ArrayList k = new ArrayList(); k.add(root.val); ArrayList> kk = new ArrayList>(); kk.add(k); return kk; } ArrayList> res1 = pathSum(root.left,sum-root.val); ArrayList> res2 = pathSum(root.right,sum-root.val); res1.addAll(res2); for(ArrayList e : res1 ){ e.add(0,root.val); } return res1; } }