LeetCode 437. Path Sum III (STL map前缀和)
找遍所有路径,特判以根为起点的串即可。
代码:
/**
* 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前缀和)的更多相关文章
- 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 ...
- [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 ...
- 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 ...
- leetcode 437 Path Sum III 路径和
相关问题:112 path sum /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNo ...
- 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 ...
- 【leetcode】437. Path Sum III
problem 437. Path Sum III 参考 1. Leetcode_437. Path Sum III; 完
- 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 ...
- 437. Path Sum III
原题: 437. Path Sum III 解题: 思路1就是:以根节点开始遍历找到适合路径,以根节点的左孩子节点开始遍历,然后以根节点的右孩子节点开始遍历,不断循环,也就是以每个节点为起始遍历点 代 ...
- [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 ...
随机推荐
- 关于swift构造方法
switf 中如果遇到这样的错,,,大概错误就是,"必须要调用父类的构造方法",,可是呢,,调用了super.init() 不就是调用了构造方法了吗? 结果上去一查,,结果一名外 ...
- 压力测试工具 Tinyget
Tinyget 压力测试工具使用方法为:命令行切换到工具所在路径下,然后输入压力命令.如:tinyget -srv:localhost -uri:/FeaturedProdu1cts.aspx -th ...
- 多线程与MySQL(十)
1.1 多线程 在传统操作系统中,每个进程有一个地址空间,而且默认就有一个控制线程 线程顾名思义,就是一条流水线工作的过程,一条流水线必须属于一个车间,一个车间的工作过程是一个进程 车间负责把资源整合 ...
- 什么是 Dropout
为了应对神经网络很容易过拟合的问题,2014年 Hinton 提出了一个神器, **Dropout: A Simple Way to Prevent Neural Networks from Over ...
- SQL基本语句:1.模式 3.索引
每次很长时间不用sql语句之后,都需要把基础的捡一捡,索性做个笔记,以后可以长看
- Mac-O文件加载的全过程(一)
在Mac的开发中, 有没有想过当我们点击可执行文件之后,Mac究竟做了什么事情才让我们的程序运行起来? 对于应用层开发人员或者普通的用户而言, 其实无需知道的这么详细:但是对于内核开发人员而言, 如果 ...
- Xcode7 下导入第三方库 图文介绍
网上没有很好的图文介绍,干脆我自己写一个好了,方便新手入门. 这里以导入著名的第三方网络库AFNetWorking v3.0.4和数据库FMDB v2.6.2为例进行说明. 好,下面开始. 下载源文件 ...
- Prime Distance POJ - 2689 线性筛
一个数 $n$ 必有一个不超过 $\sqrt n$ 的质因子. 打表处理出 $1$ 到 $\sqrt n$ 的质因子后去筛掉属于 $L$ 到 $R$ 区间的素数即可. Code: #include&l ...
- 初见UDP_Client
from socket import *ip_prot = ('192.168.55.1',8080)buffer_size = 1024udp_client = socket(AF_INET,SOC ...
- [洛谷P3391]【模板】文艺平衡树(Splay)
题目大意:给定一个$1\sim n$的序列,每次翻转一个区间,输出最后的序列. 解题思路:Splay的区间翻转操作.我借此打了个Splay的模板(运用内存池,但有些功能不确定正确,例如单点插入). 大 ...