2ms Java solution beats 96%


  • 2
    S
        public List<List<Integer>> pathSum(TreeNode root, int sum) {
            List<List<Integer>> res = new ArrayList<>();
            if (root == null) {
                return res;
            }
            dfs(root, sum, new ArrayList<>(), res);
            return res;
        }
        
        private void dfs(TreeNode root, int sum, List<Integer> crt, List<List<Integer>> res) {
            if (root == null) return;
            sum -= root.val;
            crt.add(root.val);
            if (root.left == null && root.right == null && sum == 0) {
                res.add(new ArrayList<>(crt));
            }
            dfs(root.left, sum, crt, res);
            dfs(root.right, sum, crt, res);
            crt.remove(crt.size() - 1);
        }
    

Log in to reply
 

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