https://leetcode.com/problems/path-sum-iii/

理解比较困难,可以先看https://www.cnblogs.com/albert67/p/10416402.html

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public int pathSum(TreeNode root, int sum) {
HashMap<Integer,Integer> preSum=new HashMap<>();
preSum.put(0,1);// 当键值等于0,说明curSum==sum,所以把设为1
return helper(root,0,sum,preSum);
} public int helper(TreeNode n,int curSum,int target,HashMap<Integer,Integer> preSum){
if(n==null)return 0;
curSum+=n.val;
int res=preSum.getOrDefault(curSum-target,0);
preSum.put(curSum,preSum.getOrDefault(curSum,0)+1);
res+=helper(n.left,curSum,target,preSum)+helper(n.right,curSum,target,preSum);
preSum.put(curSum,preSum.get(curSum)-1);
return res;
}
}

递归比较慢,但很容易理解

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

leetcode437--Path Sum III的更多相关文章

  1. 第34-3题:LeetCode437. Path Sum III

    题目 二叉树不超过1000个节点,且节点数值范围是 [-1000000,1000000] 的整数. 示例: root = [10,5,-3,3,2,null,11,3,-2,null,1], sum ...

  2. 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 ...

  3. 【leetcode】437. Path Sum III

    problem 437. Path Sum III 参考 1. Leetcode_437. Path Sum III; 完

  4. 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 ...

  5. 437. Path Sum III

    原题: 437. Path Sum III 解题: 思路1就是:以根节点开始遍历找到适合路径,以根节点的左孩子节点开始遍历,然后以根节点的右孩子节点开始遍历,不断循环,也就是以每个节点为起始遍历点 代 ...

  6. LeetCode_437. Path Sum III

    437. Path Sum III Easy You are given a binary tree in which each node contains an integer value. Fin ...

  7. [LeetCode] Path Sum III 二叉树的路径和之三

    You are given a binary tree in which each node contains an integer value. Find the number of paths t ...

  8. LeetCode 437. Path Sum III (路径之和之三)

    You are given a binary tree in which each node contains an integer value. Find the number of paths t ...

  9. LeetCode算法题-Path Sum III(Java实现)

    这是悦乐书的第227次更新 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第94题(顺位题号是437).您将获得一个二叉树,其中每个节点都包含一个整数值.找到与给定值相加的路径数 ...

  10. [LeetCode] 437. Path Sum III 路径和 III

    You are given a binary tree in which each node contains an integer value. Find the number of paths t ...

随机推荐

  1. 装饰器,栈 ,asyncio 代码

    装饰器目的: 不改变原来代码的基础上. 给函数添加新功能动态代理. 拦截器 通用装饰器的写法def wrapper(fn): def inner(*args, **kwargs): '''之前''' ...

  2. 多端统一框架尝试--Taro

    参考资料 Taro官网Taro GitHubTaro资源汇总Taro-UI 我的demo代码 github地址 Taro介绍和尝试心得 Taro是基于React语法规范开发的多端统一的框架,一套代码可 ...

  3. oracle12 安装

    oracle    oracle orcl    orcl

  4. mac无密登陆到linux

    最近弄了台linux云服务器,然而每次登陆linux都好麻烦,所以倒腾了下ssh无密登陆. linux:centos 6.5,自带ssh mac:yosemite,自带ssh 步骤: 1. 创建key ...

  5. Maven下CXF的wsdl2java生成客户端代码

    <plugin> <groupId>org.apache.cxf</groupId> <artifactId>cxf-codegen-plugin< ...

  6. SpringBoot 之jsp

    Boot 内嵌的tomcat 是不支持jsp 的, jetty 也是. 虽然boot也是有默认配置一个InternalResourceViewResolver ,但是它并不像我们在springmvc ...

  7. SQL Server截取字符串

    --SQL Server截取字符串 , Len('hello@163.com')) ,charindex('.','hello@163.com'))

  8. 移动平台MOBA发热与帧率优化

    MOBA项目的优化进入到了第二阶段,千元机,发热严重问题处理,及帧率进一步提升. 回顾之前的优化,当初我的 OPPO R9S不过8-10帧,后来经过了逻辑计算的一些优化后达到10-20帧. 再后来开启 ...

  9. javascript函数嵌套时arguments的问题

    疑问: var funtest = function () { var fun = function (val, val2) { alert(arguments.length); //此处答案? 有些 ...

  10. office 安装

    在 gaobo百度云下载安装包. 自定义安装,并在自定义界面选择安装路径. 破解: