1. /**
  2. * Definition for a binary tree node.
  3. * struct TreeNode {
  4. * int val;
  5. * TreeNode *left;
  6. * TreeNode *right;
  7. * TreeNode(int x) : val(x), left(NULL), right(NULL) {}
  8. * };
  9. */
  10. class Solution {
  11. public:
  12. int pathSum(TreeNode* root, int sum) {
  13. if (root == NULL)
  14. return ;
  15. return Sum(root, , sum) + pathSum(root->left, sum) + pathSum(root->right, sum);
  16. }
  17. private:
  18. //pre为前面节点的和,cur为前面加上现在遍历到的节点;
  19. int Sum(TreeNode* root, int pre, int sum){
  20. if (root == NULL)
  21. return ;
  22. int cur = pre + root->val;
  23.  
  24. return (cur == sum) + Sum(root->left, cur, sum) + Sum(root->right, cur, sum);
  25. }
  26. };
  1. root = [10,5,-3,3,2,null,11,3,-2,null,1], sum = 8
  2.  
  3. 10
  4. / \
  5. 5 -3
  6. / \ \
  7. 3 2 11
  8. / \ \
  9. 3 -2 1
  10.  
  11. Return 3. The paths that sum to 8 are:
  12.  
  13. 1. 5 -> 3
  14. 2. 5 -> 2 -> 1
  15. 3. -3 -> 11

You are given a binary tree in which each node contains an integer value.

Find the number of paths that sum to a given value.

The path does not need to start or end at the root or a leaf, but it must go downwards (traveling only from parent nodes to child nodes).

The tree has no more than 1,000 nodes and the values are in the range -1,000,000 to 1,000,000.

【easy】437. Path Sum III 二叉树任意起始区间和的更多相关文章

  1. 【easy-】437. Path Sum III 二叉树任意起始区间和

    /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode ...

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

  3. 437. Path Sum III

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

  4. 【leetcode】437. Path Sum III

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

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

  6. [LeetCode] Path Sum III 二叉树的路径和之三

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

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

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

  9. leetcode 437 Path Sum III 路径和

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

随机推荐

  1. PHP二维数组去重(指定键名)

    本例对二维数组中某个指定键名进行去重,发现网上大多使用array_unique或是foreach遍历等方式去重,效果不佳且较为繁琐. 本文以二维数组去重为引,介绍array_column函数的三种用法 ...

  2. 函数的创建与区别和 prototype

    https://www.cnblogs.com/haoxl/p/5267724.html(copy) https://www.cnblogs.com/loveyoume/p/6112044.html( ...

  3. CentOS7 64位下MySQL5.7安装与配置(YUM)

    1.配置YUM源 在MySQL官网中下载YUM源rpm安装包:http://dev.mysql.com/downloads/repo/yum/ # 下载mysql源安装包 shell> wget ...

  4. iOS 枚举 初体验

    iOS枚举 我的code /*文件名 SC_CDV_OCR.m*/ typedef enum _OCRResultState { OCRResultStateOK = 1, OCRResultStat ...

  5. hashChange & url change & QRCode & canvas to image

    hashChange & url change & QRCode & canvas to image "use strict"; /** * * @auth ...

  6. monkeyrunner简介

    monkeyrunner简介 MonkeyRunner工具是使用Jython(使用Java编程语言实现的Python)写出来的,它提供了多个API,通过monkeyrunner API 可以写一个Py ...

  7. codevs 2370 小机房的树(LCA)

    过了这么长的时间终于开始看LCA了... 有一次训练题卡在LCA当时不会...拖了好久好久...其实现在还是不会... 只会tarjan... 传送门 板子题咯 tarjan的算法就是基于先序遍历的顺 ...

  8. C#中声明、调用和配置事件的演示源码

    下面的内容是关于C#中声明.调用和配置事件的演示的内容,应该能对大伙有些好处. using System;namespace MyCollections { using System.Collecti ...

  9. luogu4365 秘密袭击 (生成函数+线段树合并+拉格朗日插值)

    求所有可能联通块的第k大值的和,考虑枚举这个值: $ans=\sum\limits_{i=1}^{W}{i\sum\limits_{S}{[i是第K大]}}$ 设cnt[i]为连通块中值>=i的 ...

  10. GraphQL

    GraphQL 官方描述: GraphQL 既是一种用于 API 的查询语言也是一个满足你数据查询的运行时. GraphQL 对你的 API 中的数据提供了一套易于理解的完整描述,使得客户端能够准确地 ...