Failed on time limit


  • 0
    T
    public class Solution {
    public List<List<Integer>> L =new LinkedList<List<Integer>>();
    public List<List<Integer>> pathSum(TreeNode root, int sum) {
        if(root==null) return null;
        findLists(new LinkedList<Integer>(),root,sum);
        return L;
    }
    
    void findLists(LinkedList<Integer> l, TreeNode x, int sum){
        if(x==null) return;
        sum-=x.val;
        l.add(x.val);
        if(x.right==null && x.left==null && sum==0){
            L.add(l);
            return;
        }
        findLists(l,x.right,sum);
        findLists(l,x.left,sum);
    
    }
    

    Recursively go through all paths, use list to save paths, when it reaches to a qualified path, put this path to the result.
    How could I improve the complexity?


Log in to reply
 

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