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. Ubuntu16.04下修改MySQL数据的默认存储位置

    由于在Linux下MySQL默认是存储在/var/lib/mysql目录下,mysql的数据会非常大,由于/var所划分的空间不够大,所以我们需要将mysql数据存放路径修改一下,放到大分区里面,以便 ...

  2. python下selenium自动化测试自我实践

    周末实验自动化提交数据时,本来没打算写记录的,不过遇到一些问题,觉得可以提提.基本操作就不用写了,搜索过程中都发现了两个博客都出了selenium+python的书,说明操作一搜一大把. 1. 等待页 ...

  3. VS2010 永久配置OpenCv2.4.9 及转换到COFF 期间失败:文件无效或损坏,解决方法

    1.下载OpenCv2.4.9(win pack):http://opencv.org/releases.html 下载完成后,进行解压(win7 64位系统) 2.环境配置,配置如下图所示: 找到p ...

  4. python-反射案例讲解

    login.py#!/usr/bin/dev python# coding:utf-8 def index(): print u'欢迎访问xx网站首页' def login(): print u'登录 ...

  5. 创建Flask实例对象时的参数和app.run()中的参数

    app=Flask(name,static_folder=“static”,static_url_path="/aaa",template_folder=“templates”) ...

  6. JEECG 上传插件升级-Online

    前言: 现有的uploadify上传是基于swf的,随着H5的普及,flash即将退出历史舞台,JEECG本着与时俱进的原则,将全面升级JEECG系统中的上传功能,采用新式上传插件plupload,此 ...

  7. jq动画分析1

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  8. 工程C++基础

    大家好,我是老A.今天我们要学习的是工程C++,这是一个不可描述的东西.我主要讲的是template. template的用处是装逼,所以很重要. C++配备了函数模板和类模板.函数模板就是我们平时的 ...

  9. Vue.js连接后台数据jsp页面  ̄▽ ̄

    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...

  10. JAVA算术运算符

    算术运算符 +.-.*(乘号)./(除号,取商).%(取余数.取模) 特殊的运算符:++(加加).--(减减) int  a = 1; a+=10;  等同于a=a+10; a-=5;   等同于a= ...