Why i got time limit error ?


  • 0
    A
    public class Solution {
        public List<List<Integer>> pathSum(TreeNode root, int sum) {
            List<List<Integer>> rst = new ArrayList<List<Integer>>();
            List<Integer> lst = new ArrayList<Integer>();
            pathSumHelper(root, sum, lst, rst);
            return rst;
        }
        
        public void pathSumHelper(TreeNode root, int sum, List<Integer> list, List<List<Integer>> result){
            if(root == null) return;
            
            int curSum = sum - root.val;
            list.add(root.val);
            
            if(root.left == null && root.right == null && curSum == 0) {
                result.add(list);
            }
            if(root.left != null)  pathSumHelper(root.left, curSum, list, result);
            if(root.right!= null)  pathSumHelper(root.right, curSum, list, result);
            return ;
        }
    }

Log in to reply
 

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