leetcode 437. Path Sum III
来源:互联网 发布:软件app下载 编辑:程序博客网 时间:2024/06/16 08:05
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 1Return 3. The paths that sum to 8 are:1. 5 -> 32. 5 -> 2 -> 13. -3 -> 11我的想法是,使用preList来存储 与当前结点在同一路径下的 之前走过的 结点值。
public class Path_Sum_III_437 {int count=0;public int pathSum(TreeNode root, int sum) {List<Integer> list=new ArrayList<Integer>();DFS(root,list,sum);return count;}public void DFS(TreeNode root,List<Integer> prelist,int sum){if(root==null){return;}if(root.val==sum){count+=1;}int pre=0;for(int i=prelist.size()-1;i>=0;i--){pre+=prelist.get(i);if(pre+root.val==sum){count+=1;}}prelist.add(root.val);DFS(root.left,prelist,sum);DFS(root.right,prelist,sum);prelist.remove(prelist.size()-1);}}
大神用了一个更加简洁的DFS:
还有大神用了map:
1. preSum存储了从根结点到当前结点的所有” prefix sum “。
2. map存储的是 <prefix sum, frequency> pairs before getting to the current node。
使用hashmap来存储 ( key : 之前的sum,value : 多少种路径到达之前的sum) , 之后,当从根结点走到A点时,判断是否有以A结尾的路径使得sum=target,就看 “从根结点到~A结点的和” 减去 “之前的和” 是否等于target。如题干中的例子,当走到左边第一个3结点时,发现有18-10=8,就知道5->3这个路径符合要求。
再举个例子 :
在 1,2,-1,-1,2 路径中,计算得到的 “之前的sum"是:1, 3, 2, 1, 3, 假定我们的target和是 2 ,那么一共的路径有{2}, {1,2,-1}, {2,-1,-1,2} 和 {2}这几种方式。求。
以第一个2结尾的路径:currSum=3,发现有preSum=1,满足,路径为 2 (去掉了根结点1)
以第一个-1结尾的路径:currSum=2,发现有preSum=0(在未递归前的初始化时 插入的),满足,路径为 1->2->-1(从根结点开始)
以最后一个2结尾的路径:currSum=3,发现有两个preSum=1,满足,路径为 2,-1,-1,2(去掉了根结点1) 和 2(去掉了1->2->-1>-1)。
public int pathSum(TreeNode root, int sum) { HashMap<Integer, Integer> preSum = new HashMap(); preSum.put(0,1); helper(root, 0, sum, preSum); return count;}int count = 0;public void helper(TreeNode root, int currSum, int target, HashMap<Integer, Integer> preSum) { if (root == null) { return; } currSum += root.val; if (preSum.containsKey(currSum - target)) { count += preSum.get(currSum - target); } if (!preSum.containsKey(currSum)) { preSum.put(currSum, 1); } else { preSum.put(currSum, preSum.get(currSum)+1); } helper(root.left, currSum, target, preSum); helper(root.right, currSum, target, preSum); preSum.put(currSum, preSum.get(sum) - 1);}
- LeetCode 437. Path Sum III
- LeetCode 437. Path Sum III
- LeetCode 437. Path Sum III
- leetcode 437. Path Sum III
- [LeetCode]437. Path Sum III
- 【LeetCode】 437. Path Sum III
- [leetcode]437. Path Sum III
- LeetCode 437. Path Sum III
- [leetcode]437. Path Sum III
- [LeetCode]437. Path Sum III
- Leetcode-437. Path Sum III
- Leetcode-437. Path Sum III
- LeetCode-437. Path Sum III
- 【Leetcode】437. Path Sum III
- 【Leetcode】437. Path Sum III
- Leetcode 437. Path Sum III
- [LeetCode]437. Path Sum III
- [leetcode]: 437. Path Sum III
- detection of nose
- idea 配置注释模板
- 170810 Python-封装RouterScan的DLL
- Linux设备树语法详解
- Fun House CSU
- leetcode 437. Path Sum III
- JAVA泛型的使用技巧与代码的封装
- 排序算法总结二(JavaScript)
- jQuery(3)__jQuery基础<事件>
- OpenJudge百炼-2712-细菌繁殖-C语言-日期处理
- C++ 下文件的读取和写入
- python序列
- 1026. 程序运行时间
- 对攻读计算机研究生的看法(差距还是有的)