Skip to content

Latest commit

 

History

History
112 lines (98 loc) · 2.96 KB

437. Path Sum III.md

File metadata and controls

112 lines (98 loc) · 2.96 KB

leetcode Daily Challenge on August 8th, 2020.


Difficulty : Medium

Related Topics : Tree


You are given a binary tree in which each node contains an integer value.

Find the number of paths that sum to a given value.

The path does not need to start or end at the root or a leaf, but it must go downwards (traveling only from parent nodes to child nodes).

The tree has no more than 1,000 nodes and the values are in the range -1,000,000 to 1,000,000.

Example:

root = [10,5,-3,3,2,null,11,3,-2,null,1], sum = 8

      10
     /  \
    5   -3
   / \    \
  3   2   11
 / \   \
3  -2   1

Return 3. The paths that sum to 8 are:

1.  5 -> 3
2.  5 -> 2 -> 1
3. -3 -> 11

Solution

  • mine
    • Java
      • DFS Runtime: 27 ms, faster than 14.68%, Memory Usage: 40 MB, less than 30.10% of Java online submissions
        int ans = 0;
        public int pathSum(TreeNode root, int sum) {
            dfs(root, sum);
            return ans;
        }
        
        List<Integer> dfs(TreeNode node, int sum) {
            if (node == null) return null;
            List<Integer> left = new ArrayList<>();
            List<Integer> right = new ArrayList<>();
            if (node.left != null) left = dfs(node.left, sum);
            if (node.right != null) right = dfs(node.right, sum);
        
            List<Integer> res = new ArrayList<>();
            if (left.size() > 0 && right.size() > 0) {
                for (int i : left) {
                    res.add(i + node.val);
                    if (i + node.val == sum) ans++;
                }
                for (int i : right) {
                    if (i + node.val == sum) ans++;
                    res.add(i + node.val);
                }
            } else {
                left = left.size() == 0 ? right : left;
                for (int i : left) {
                    if (i + node.val == sum) ans++;
                    res.add(node.val + i);
                }
            }
            res.add(node.val);
            if (node.val == sum) ans++;
            return res;
        }
        

  • the most votes
  • Runtime: 2 ms, faster than 100.00%, Memory Usage: 39.1 MB, less than 82.13% of Java online submissions
    public int pathSum(TreeNode root, int sum) {
        HashMap<Integer, Integer> preSum = new HashMap();
        preSum.put(0,1);
        return helper(root, 0, sum, preSum);
    }
    
    public int helper(TreeNode root, int currSum, int target, HashMap<Integer, Integer> preSum) {
        if (root == null) {
            return 0;
        }
    
        currSum += root.val;
        int res = preSum.getOrDefault(currSum - target, 0);
        preSum.put(currSum, preSum.getOrDefault(currSum, 0) + 1);
    
        res += helper(root.left, currSum, target, preSum) + helper(root.right, currSum, target, preSum);
        preSum.put(currSum, preSum.get(currSum) - 1);
        return res;
    }