Leetcode: 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 , nodes and the values are in the range -,, to ,,. Example: root = [,,-,,,null,,,-,null,], sum = / \
-
/ \ \ / \ \
- Return . The paths that sum to are: . ->
. -> ->
. - ->
Add the prefix sum to the hashMap, and check along path if hashMap.contains(pathSum+cur.val-target);
My Solution
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public int pathSum(TreeNode root, int sum) {
if (root == null) return 0;
ArrayList<Integer> res = new ArrayList<Integer>();
res.add(0);
HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
map.put(0, 1);
helper(root, sum, 0, res, map);
return res.get(0);
} public void helper(TreeNode cur, int target, int pathSum, ArrayList<Integer> res, HashMap<Integer, Integer> map) {
if (map.containsKey(pathSum+cur.val-target)) {
res.set(0, res.get(0) + map.get(pathSum+cur.val-target));
}
if (!map.containsKey(pathSum+cur.val)) {
map.put(pathSum+cur.val, 1);
}
else map.put(pathSum+cur.val, map.get(pathSum+cur.val)+1);
if (cur.left != null) helper(cur.left, target, pathSum+cur.val, res, map);
if (cur.right != null) helper(cur.right, target, pathSum+cur.val, res, map);
map.put(pathSum+cur.val, map.get(pathSum+cur.val)-1);
}
}
一个更简洁的solution: using HashMap to store ( key : the prefix sum, value : how many ways get to this prefix sum) , and whenever reach a node, we check if prefix sum - target exists in hashmap or not, if it does, we added up the ways of prefix sum - target into res.
public int pathSum(TreeNode root, int sum) {
Map<Integer, Integer> map = new HashMap<>();
map.put(0, 1); //Default sum = 0 has one count
return backtrack(root, 0, sum, map);
}
//BackTrack one pass
public int backtrack(TreeNode root, int sum, int target, Map<Integer, Integer> map){
if(root == null)
return 0;
sum += root.val;
int res = map.getOrDefault(sum - target, 0); //See if there is a subarray sum equals to target
map.put(sum, map.getOrDefault(sum, 0)+1);
//Extend to left and right child
res += backtrack(root.left, sum, target, map) + backtrack(root.right, sum, target, map);
map.put(sum, map.get(sum)-1); //Remove the current node so it wont affect other path
return res;
}
Leetcode: Path Sum III的更多相关文章
- [LeetCode] Path Sum III 二叉树的路径和之三
You are given a binary tree in which each node contains an integer value. Find the number of paths t ...
- Python3解leetcode Path Sum III
问题描述: You are given a binary tree in which each node contains an integer value. Find the number of p ...
- 第34-3题:LeetCode437. Path Sum III
题目 二叉树不超过1000个节点,且节点数值范围是 [-1000000,1000000] 的整数. 示例: root = [10,5,-3,3,2,null,11,3,-2,null,1], sum ...
- 47. leetcode 437. Path Sum III
437. Path Sum III You are given a binary tree in which each node contains an integer value. Find the ...
- 【leetcode】437. Path Sum III
problem 437. Path Sum III 参考 1. Leetcode_437. Path Sum III; 完
- leetcode 112. Path Sum 、 113. Path Sum II 、437. Path Sum III
112. Path Sum 自己的一个错误写法: class Solution { public: bool hasPathSum(TreeNode* root, int sum) { if(root ...
- [LeetCode] Path Sum II 二叉树路径之和之二
Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...
- [LeetCode] Path Sum 二叉树的路径和
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...
- [LeetCode] Path Sum IV 二叉树的路径和之四
If the depth of a tree is smaller than 5, then this tree can be represented by a list of three-digit ...
随机推荐
- Hightcharts动态创建series
第一种方法: 申明options时动态设置series,然后再创建chart对象 代码如下: <html> <head> <title>Highcharts Exa ...
- java修改request的paramMap
最近做项目,发现要修改request的参数内容.因为想要在request的paramMap里面默认注入,modifier和modifierName,这些内容.但是这个Map是不能修改的.所以采用了如下 ...
- StoryBoard--看上去很美
StoryBoard--看上去很美 介绍 StoryBoard 是苹果在 2011 年的 WWDC Session 309<Introducing Interface Builder Story ...
- ado.net 实体类_数据访问类
实体类: using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ...
- [转]CPU的位数与操作系统的位数的区别
转自:http://weiheyouchou.blog.hexun.com/35564976_d.html 随着近来AMD和Intel的64位CPU以及 Microsoft 64位操作系统的相继发布, ...
- ssh问题
一.基于秘钥认证: ssh-keygen ssh-copy-id -i .ssh/id_rsa.pub 10.10.10.70#在对方authorized_keys 文件中写入自己的id_rsa.pu ...
- html图片和文字的细节
ul中的每一个li如果里面添加“一个图,一行字”, 这样图片会紧贴在左侧,而文字会居中,这两个元素不会紧贴着. 产生这种问题的原因我推测是:我图片设置了左浮动,但文字没有设置浮动,而一旦将文字设置为浮 ...
- c#面向对象基础 静态成员、构造函数、命名空间与类库
静态成员 属性.方法和字段等成员是对象实例所特有的,即改变一个对象实例的这些成员不影响其他的实例中的这些成员.除此之外,还有一种静态成员(也称为共享成员),例如静态方法.静态属性或静态字段.静态成员可 ...
- Learn ZYNQ (3)
移植android3.3到ZedBoard follow doc:Android移植Guide1.3.pdf follow website: http://elinux.org/Zedboard_An ...
- zju(4)使用busybox制作根文件系统
1.实验目的 1.学习和掌握busybox相关知识及应用: 2.学会使用交叉编译器定制一个busybox: 3.利用该busybox制作一个文件系统: 4.熟悉根文件系统组织结构: 5.定制.编译ra ...