Why my solution costs 5ms. I saw many of yours just cost 3ms. What's the problem?


  • 0
    Y
    public List<List<Integer>> pathSum(TreeNode root, int sum) {
        List<List<Integer>> list = new LinkedList<List<Integer>>();
        Stack<Integer> temp = new Stack<Integer>();
        addPath(root, sum, list, temp);
        return list;
    }
    public void addPath(TreeNode node, int sum, List<List<Integer>> list, Stack temp){
        if(node==null) return;
        temp.push(node.val);
        if(node.val==sum&&node.left==null&&node.right==null) list.add(new LinkedList<Integer>(temp));
        else{
            addPath(node.left, sum-node.val, list, temp);
            addPath(node.right, sum-node.val, list, temp);
        }
        temp.pop();
    }

  • 0
    E

    Same thing here. I doubt if there is anything wrong with your code, but that leetcode either increased test sample size or deteriorate their server (which is not quite likely).


Log in to reply
 

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