zoukankan      html  css  js  c++  java
  • 437. Path Sum III

    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

    public class Solution {
        public int pathSum(TreeNode root, int sum) {
            if (root == null) return 0;
            return pathSumFrom(root, sum) + pathSum(root.left, sum) + pathSum(root.right, sum);
        }
        
        private int pathSumFrom(TreeNode node, int sum) {
            if (node == null) return 0;
            return (node.val == sum ? 1 : 0) 
                + pathSumFrom(node.left, sum - node.val) + pathSumFrom(node.right, sum - node.val);
        }
    }

    巧啊,给pathsum自己call自己,这样相当于在root.left, root.rigtht处从上到下从root到val,然后pathsumfrom就正常从root到null即可

    public int pathSum(TreeNode root, int sum) {
            if (root == null) {
                return 0;
            }
            Map<Integer, Integer> map = new HashMap<>();
            map.put(0, 1);
            return findPathSum(root, 0, sum, map);  
        }
        private int findPathSum(TreeNode curr, int sum, int target, Map<Integer, Integer> map) {
            if (curr == null) {
                return 0;
            }
            // update the prefix sum by adding the current val
            sum += curr.val;
            // get the number of valid path, ended by the current node
            int numPathToCurr = map.getOrDefault(sum-target, 0); 
            // update the map with the current sum, so the map is good to be passed to the next recursion
            map.put(sum, map.getOrDefault(sum, 0) + 1);
            // add the 3 parts discussed in 8. together
            int res = numPathToCurr + findPathSum(curr.left, sum, target, map)
                                                   + findPathSum(curr.right, sum, target, map);
           // restore the map, as the recursion goes from the bottom to the top
            map.put(sum, map.get(sum) - 1);
            return res;
        }

    https://leetcode.com/problems/path-sum-iii/discuss/91878/17-ms-O(n)-java-Prefix-sum-method

    用prefix sum,prefix sum是从root加到当前node的sum,那么我们可以想到设x,然后prefix sum = x + target的话,x =  prefixsum - target,其中x是从root到当前node中的一点,这样的话从这个x点到cur node的和就是target了。

    然后又因为x在map中,所以x的频率就是root到当前node能形成target sum的数量。随后更新sum的频率,继续dfs,这个点dfs完后要进行剪枝(因为这个点的 情况考虑完了,所以不要再用这个点了)

    return res

  • 相关阅读:
    pod hook 钩子函数 生命周期 lifecycle/postStart/preStop
    yaml基本语法 && 静态pod && pod生命周期 && pod健康检测
    netsh winsock reset
    H3C Password Contrpol
    H3C ssh 配置参考
    win7怎么修复winsock被删除
    pod健康检测 exec/http/tcpSocket
    win10 1709 新装 鼠标卡顿
    shell 标准输入输出及错误输出 重定向
    IP安全策略只允许指定IP连接远程桌面,限制IP登
  • 原文地址:https://www.cnblogs.com/wentiliangkaihua/p/13388831.html
Copyright © 2011-2022 走看看