找遍所有路径,特判以根为起点的串即可。

代码:

/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int ans = ;
map <int, int> m;
int pathSum(TreeNode* root, int sum) {
findSum(root, sum, );
return ans;
} void findSum(TreeNode *node, int sum, int num){
if(node == NULL) return;
num += node->val;
if(num == sum) ans += m[]+;
else ans += m[num-sum];
m[num]++;
findSum(node->left, sum, num);
findSum(node->right, sum, num);
m[num]--;
} };

LeetCode 437. Path Sum III (STL map前缀和)的更多相关文章

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

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

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

  4. leetcode 437 Path Sum III 路径和

      相关问题:112 path sum /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNo ...

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

  6. 【leetcode】437. Path Sum III

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

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

  8. 437. Path Sum III

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

  9. [LeetCode] 437. Path Sum III_ Easy tag: DFS

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

随机推荐

  1. KCP kcptun

    简介 KCP是一个快速可靠协议,能以比 TCP浪费10%-20%的带宽的代价,换取平均延迟降低 30%-40%,且最大延迟降低三倍的传输效果.纯算法实现,并不负责底层协议(如UDP)的收发,需要使用者 ...

  2. 主流的Python领域和框架--转

    原文地址:https://www.zhihu.com/question/19899608

  3. Asp.Net Core部署到Linux服务器

    从2016年7月, .NET Core1.0 正式发布开始,由于时间问题,我没怎么关注过.NET Core,最近刚抽出点时间研究了下,先讲下如何把ASP.NET Core部署到Linux上吧.这里我用 ...

  4. FluentAPI配置

    基本 EF 配置只要配置实体类和表.字段的对应关系.表间关联关系即可. 如何利用 EF的高级配置,达到更多效果:如果数据错误(比如字段不能为空.字符串超长等),会在 EF 层就会报错,而不会被提交给数 ...

  5. WebView简单用法

    1.空布局 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:andr ...

  6. ViewPager滑动到最后一页再向左滑动进入主界面

    在OnPageChangeListener中的onPageScrolled方法里判断 @Override public void onPageScrolled(int arg0, float arg1 ...

  7. jsp基本内置标签

    <jsp:foward page="url"> <jsp:param value=" " name=" "/> &l ...

  8. [THUWC2017]在美妙的数学王国中畅游 LCT+泰勒展开+求导

    p.s. 复合函数求导时千万不能先带值,再求导. 一定要先将符合函数按照求导的规则展开,再带值. 设 $f(x)=g(h(x))$,则对 $f(x)$ 求导: $f'(x)=h'(x)g'(h(x)) ...

  9. freeswitch 一些坑

    1.You must install libopus-dev to build mod_opus. Stop. 确实已经安装libopus-dev后 将文件 usr/local/src/mod/cod ...

  10. python_传递任意数量的实参

    '''def name(*args): #python创建一个空元组,将收到的所有值都封装在这个元组中 """打印所有姓名""" for i ...