GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — Leetcode-Java/leetcode/solution/src/PathSumIII.java at master · Tulpen/Leetcode-Java Expand file tree/ Copy pathPathSumIII.javaMore file actions 82 lines (67 loc) · 2.27 KB / Copy pathPathSumIII.javaFile metadata and controls82 lines (67 loc) · 2.27 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 import java.util.HashMap; public class PathSumIII { /** * 当root.val == Sum时,不要return,因为继续往下走可能有路径刚好加起来为0,典型的为[1,-2,1,-1],目标和为-1 * 这里隐藏了四条路径,[1,-2], [-2,1], [-1], [1,-2,1,-1],如果在[1,-2]就return了,那就会掉了[1,-2,1,-1] * 可参考https://discuss.leetcode.com/category/562/path-sum-iii */ /** * 有两种可能,算上当前root和不算当前root */ public int pathSum(TreeNode root, int sum) { int[] count = new int[1]; helperSum(root, sum, count); return count[0]; } /** * 既可以算上,也可以不算上root */ private void helperSum(TreeNode root, int sum, int[] count) { if (root == null) { return; } // 算上root helper(root, sum, count); // 不算上root helperSum(root.left, sum, count); helperSum(root.right, sum, count); } /** * 算上root */ private void helper(TreeNode root, int sum, int[] count) { if (root == null) { return; } if (root.val == sum) { count[0]++; // 这里不用返回,因为下面的路径和可能为0; } helper(root.left, sum - root.val, count); helper(root.right, sum - root.val, count); } /** * 更优化的写法,时间复杂度O(n) * 和Two Sum较类似,都是算出从root到当前节点的和,target为两段和的差值 */ public int pathSum2(TreeNode root, int sum) { HashMap<Integer, Integer> map = new HashMap<>(); map.put(0, 1); int[] count = new int[1]; helper(root, sum, 0, map, count); return count[0]; } private void helper(TreeNode node, int target, int sum, HashMap<Integer, Integer> map, int[] count) { if (node == null) { return; } sum += node.val; if (map.containsKey(sum - target)) { count[0] += map.get(sum - target); } map.put(sum, map.getOrDefault(sum, 0) + 1); helper(node.left, target, sum, map, count); helper(node.right, target, sum, map, count); map.put(sum, map.getOrDefault(sum, 0) - 1); } } |
| You are visitor 01402775 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |