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 ...
随机推荐
- CaffeExample 在CIFAR-10数据集上训练与测试
本文主要来自Caffe作者Yangqing Jia网站给出的examples. @article{jia2014caffe, Author = {Jia, Yangqing and Shelhamer ...
- SQL 导出数据字典
用于参考: SELECT 表名=case when a.colorder=1 then d.name else '' end, 表说明=case w ...
- MongoDB在MacOS上的客户端Robo 3T 的简单使用(二)
最近写了一个用node来操作MongoDB完成增.删.改.查.排序.分页功能的示例,并且已经放在了服务器上地址:http://39.105.32.180:3333. 本篇文章只做简单介绍,能够使用起来 ...
- spring《四》自动装配
byName模式<bean autowire="byName"> Spring会查找一个叫做date的bean定义. byType模式<bean autowire ...
- eclipse 配置 tomcat 时候的一些注意事项(随机更新)
1,一些常用的设置,如下图,不特别说明了,看标记应该就知道注意事项了 2,配置文件的问题.eclipse里面如下图的配置文件里如果有所改动,那么在eclipse里启动Tomcat的时候,Tomcat的 ...
- BZOJ2440: [中山市选2011]完全平方数(莫比乌斯+容斥原理)
2440: [中山市选2011]完全平方数 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 4920 Solved: 2389[Submit][Sta ...
- PHP函数十进制、二进制、八进制和十六进制转换函数说明
1.十进制转二进制 decbin() 函数,如下实例 echo decbin(12); //输出 1100 echo decbin(26); //输出 11010 2.十进制转八进制 decoct( ...
- Element-ui组件--pagination分页的使用
一般在写前端页面时,经常会遇到分页这样的效果,element-ui中便有这样的插件,用vue框架使用的很方便,在此做一总结: <template> <div class=" ...
- CDR中怎么绘制一个漂亮的球衣?
cdr中怎么绘制一个漂亮的球衣?想要绘制一个漂亮的球衣,该怎么绘制呢?下面我们就来看看cdr绘制漂亮的球衣的教程,需要的朋友可以参考下: 1.画一个长方形,增加节点,移动节点,变形成如图 2.直线变曲 ...
- LayUI中实现上级下拉框动态加载下级下拉框js
js代码: var form = layui.form, layer = layui.layer; form.on("select(上级)", function(data){ va ...