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. mysql中间件kingshard

    这样写是OK的: select * from bind_history limit 10;select id, passport_id, person_id, create_time, cast(is ...

  2. mybatis入门篇:Mapper接口/关联查询/新增数据

    1.数据准备 2.编写实体类 package com.forest.owl.entity; import java.util.Date; public class User { private Lon ...

  3. Java 详解 JVM 工作原理和流程

    Java 详解 JVM 工作原理和流程 作为一名Java使用者,掌握JVM的体系结构也是必须的.说起Java,人们首先想到的是Java编程语言,然而事实上,Java是一种技术,它由四方面组成:Java ...

  4. 性能监控工具以及java堆分析OOM

      一.性能监控工具 1.系统性能监控 Linux -确定系统运行的整体状态,基本定位问题所在 -uptime: ------系统时间 ------运行时间(例子中为127天) ------连接数(每 ...

  5. scrapy-items

    items定义字段名字 import scrapy class HrItem(scrapy.Item): # define the fields for your item here like: ti ...

  6. leetcode494

    public class Solution { public int FindTargetSumWays(int[] nums, int S) { Queue<int> Q = new Q ...

  7. C#、Unity 数据类型的默认值

    using System.Collections; using System.Collections.Generic; using UnityEngine; public class Main : M ...

  8. Python常用内置函数介绍

    Python提供了一个内联模块buildin.内联模块定义了一些开发中经常使用的函数,利用这些函数可以实现数据类型的转换.数据的计算.序列的处理等功能.下面将介绍内联模块中的常用函数. Python内 ...

  9. pymongo操作mongodb

    此验证中只开启两个mongodb节点,可以连接任意节点,以下操作不涉及读写,不涉及连接那个节点 mongodb连接: from pymongo import MongoReplicaSetClient ...

  10. springboot整合mybatis遇到的那些坑

    1.接口类(指*Mapper.java)在spring中注册的问题 当控制台打印如下信息: A component required a bean named '*Mapper' that could ...