Who can help me to find the error of my code? I get the right result in eclipse, but I can't get accepted.


  • 0
    M

    public class Solution {
    List<List<Integer>> resultList = new ArrayList();
    List<Integer> subResList = new ArrayList();
    public static int sumOfNum = 0;
    public List<List<Integer>> pathSum(TreeNode root, int sum) {
    if(root == null){
    return resultList;
    }

        findList(sum, root);
        
        return resultList;
    }
    
    public void findList(int sum, TreeNode root){
        sumOfNum += root.val;
        subResList.add(root.val);
        if(root.left == null && root.right == null){
            if(sumOfNum == sum){
                List<Integer> tempList = new ArrayList<Integer>();
                for(int i=0; i<subResList.size(); i++){
                    tempList.add(subResList.get(i));
                }
                resultList.add(tempList);
                if(subResList.size() >= 1){
                     subResList.remove(subResList.size() - 1);
                }
                sumOfNum -= root.val;
                }
        }
        if(root.left != null){
            findList(sum, root.left);
        }
        if(root.right != null){
            findList(sum, root.right);
        }
    }
    

    }


Log in to reply
 

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