Easy Java Solution


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

    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.