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. 使用redis-cli --pipe快速插入数据

    具体实现步骤如下:(参考http://www.cnblogs.com/ivictor/p/5446503.html) 1. 新建一个文本文件redis_commands.txt,包含redis命令 S ...

  2. 搭建基于MySQL的读写分离工具Amoeba

    搭建基于MySQL的读写分离工具Amoeba: Amoeba工具是实现MySQL数据库读写分离的一个工具,前提是基于MySQL主从复制来实现的: 实验环境(虚拟机): 主机 角色 10.10.10.2 ...

  3. [python,2018-03-06] python中的继承顺序

    python 支持多继承,但对与经典类和新式类来说,多继承查找的顺序是不一样的.  经典类: 新式类   class P1:      def foo(self):                   ...

  4. C# 数字转换成大写

    /// <summary> /// 数字转大写 /// </summary> /// <param name="Num">数字</para ...

  5. 202. 阿里Pandora Boot

      [视频&交流平台] àSpringBoot视频:http://t.cn/R3QepWG à SpringCloud视频:http://t.cn/R3QeRZc à Spring Boot源 ...

  6. ORACLE_11G归档空间满,由于数据库装完后使用的是默认空间是闪回区

    1.首先根据alert跟踪日志发现归档空间满,路径大致如下:cd $ORACLE_BASE/diag/rdbms/jsswgsjk/jsswgsjk1/tracetail -f alert_jsswg ...

  7. fastjson中对象转化为字符串时过滤某字段

    fastjson中对象转化为字符串时过滤某字段,有两种方法: 一.在该字符定义上方添加"@JSONField(serialize=false)"注解: 二.调用含有Property ...

  8. unshift() 方法将一个或多个元素添加到数组的开头,并返回新数组的长度

    var arr = [1, 2]; arr.unshift(0); //result of call is 3, the new array length //arr is [0, 1, 2] arr ...

  9. 在delphi中XLSReadWriteII.组件的应用实例(2)

    第三方组件:XLSReadWriteII.v.5.20.67_XE3 实例源码如下:   unit Unit1; interface uses Winapi.Windows, Winapi.Messa ...

  10. asmx 接收数据过大 发生错误

    原文:https://www.cnblogs.com/xinaixia/p/5945316.html 错误:已超过传入消息(65536)的最大消息大小配额.若要增加配额,请使用相应绑定元素上的 Max ...