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
标签:node,map,return,int,root,Sum,437,III,sum 来源: https://www.cnblogs.com/wentiliangkaihua/p/13388831.html