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

代码:

/**
* 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. SQL Server的三种分页方式

    直接上代码 --top not in方式 select top 条数 * from tablename where Id not in (select top 条数*页数 Id from tablen ...

  2. c#将http调用返回额json中的有关中文的unicode转换为中文

    c#将http调用返回额json中的有关中文的unicode转换为中文 中文转Unicode:HttpUtility.UrlEncodeUnicode(string str);转换后中文格式:&quo ...

  3. maridb Error 'Operation DROP USER failed for

    数据库版本:mariadb   10.0.12 主库删除多余的用户名,因从库没有此信息造成主从故障! 报错信息如下:Error 'Operation DROP USER failed for 'use ...

  4. UIView 绘制渲染机制

    #前言 APP页面优化对小编来说一直是难题,最近一直在不断的学习和总结 ,发现APP页面优化说到底离不开view的绘制和渲染机制.网上有很多精彩的博客,小编借鉴之前N多大牛研究成果,同时结合自己遇到的 ...

  5. Object-oriented programming

    Object-oriented programming (OOP) is a programming paradigm based on the concept of "objects&qu ...

  6. 粘包解决高端_Server

    from socket import * #导入套接字模块的所有命令import subprocess #导入subprocess模块,用于执行命令行import struct #导入struck模块 ...

  7. Django路由URL

    URL配置(URLconf)就像Django所支撑网站的目录.URL与要为该URL调用的视图函数之间的映射表. URLconf配置 样式: from django.conf.urls import u ...

  8. Multipartfile与File类型相互转换

    特殊情况下需要做转换 1.M转F File file = new File(path); FileUtils.copyInputStreamToFile(multipartFile.getInputS ...

  9. laravel save() 返回 null

    原因:引用其他方法时,没有 return save()的操作结果. 在使用save()方法时,发现返回值是:null:

  10. UVALive-8072 Keeping On Track 树形dp 联通块之间缺失边的个数

    题目链接:https://cn.vjudge.net/problem/UVALive-8072 题意 给出n+1个点和n条边,每对点之间只能存在一条边. 现在要找出一个节点,使得去掉这个点后,所剩每对 ...