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

代码:

/**
* 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. POJ 1155 树形DP

    题意:电视台发送信号给很多用户,每个用户有愿意出的钱,电视台经过的路线都有一定费用,求电视台不损失的情况下最多给多少用户发送信号. 转自:http://www.cnblogs.com/andre050 ...

  2. POJ 3180 Tarjan

    题意:找强连通中点数大于2的强连通分量个数 思路:Tarjan // By SiriusRen #include <cstdio> #include <algorithm> u ...

  3. PHPMailer使用说明

    PHPMailer是一个用来发送电子邮件的函数包,远比PHP提供的mail()方便易用. 邮件格式说明 一封普通的电子邮件,通常是由发件人.收件人.抄送人.邮件标题.邮件内容.附件等内容构成.以下是一 ...

  4. struts2学习之基础笔记4

    拦截器 1.自定义拦截器类,必须继承AbstractInterceptor类(抽象类) 重写public String intercept (ActionInvocation arg0) 2.在Str ...

  5. php配置站点

    第一步:需要打开三个文件 1.C:\wamp\bin\apache\apache2.4.9\conf\httpd.conf 2.C:\wamp\bin\apache\apache2.4.9\conf\ ...

  6. Oracle数据库基础(二)

    1.表名命名规则:必须以字母开头,不能超过30个字符,不要有Oracle保留字    2.数据类型      字符型:         char :2000个字符   定长  效率高          ...

  7. Java NIO(四)文件通道

    文件通道 通道是访问I/O服务的导管,I/O可以分为广义的两大类:File I/O和Stream I/O.那么相应的,通道也有两种类型,它们是文件(File)通道和套接字(Socket)通道.文件通道 ...

  8. c#可自定义码表的base64加密解密算法类

    000 using System; using System.Collections.Generic; using System.Linq; using System.Text; using Syst ...

  9. 优动漫结合Photoshop怎么画草地?

    今天继续技法教学~草地的技法,PS教学~但是很简单,都是默认工具,而且是常用工具VS常用设置.你肯定会学会的! 草地教程,就到这里啦!有兴趣的可以尝试画一画哦,想要Get到更多有关优动漫的信息包括软件 ...

  10. 使用CablleStatement调用存储过程

    /** * 使用CablleStatement调用存储过程 * @author APPle * */ public class Demo1 { /** * 调用带有输入参数的存储过程 * CALL p ...