My simple 4ms java solution


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

Log in to reply
 

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