【easy】437. Path Sum III 二叉树任意起始区间和
- /**
- * 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 pathSum(TreeNode* root, int sum) {
- if (root == NULL)
- return ;
- return Sum(root, , sum) + pathSum(root->left, sum) + pathSum(root->right, sum);
- }
- private:
- //pre为前面节点的和,cur为前面加上现在遍历到的节点;
- int Sum(TreeNode* root, int pre, int sum){
- if (root == NULL)
- return ;
- int cur = pre + root->val;
- return (cur == sum) + Sum(root->left, cur, sum) + Sum(root->right, cur, sum);
- }
- };
- root = [10,5,-3,3,2,null,11,3,-2,null,1], sum = 8
- 10
- / \
- 5 -3
- / \ \
- 3 2 11
- / \ \
- 3 -2 1
- Return 3. The paths that sum to 8 are:
- 1. 5 -> 3
- 2. 5 -> 2 -> 1
- 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 二叉树任意起始区间和的更多相关文章
- 【easy-】437. Path Sum III 二叉树任意起始区间和
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode ...
- 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 ...
- 437. Path Sum III
原题: 437. Path Sum III 解题: 思路1就是:以根节点开始遍历找到适合路径,以根节点的左孩子节点开始遍历,然后以根节点的右孩子节点开始遍历,不断循环,也就是以每个节点为起始遍历点 代 ...
- 【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 ...
- [LeetCode] 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 (路径之和之三)
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 路径和 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 ...
随机推荐
- PHP二维数组去重(指定键名)
本例对二维数组中某个指定键名进行去重,发现网上大多使用array_unique或是foreach遍历等方式去重,效果不佳且较为繁琐. 本文以二维数组去重为引,介绍array_column函数的三种用法 ...
- 函数的创建与区别和 prototype
https://www.cnblogs.com/haoxl/p/5267724.html(copy) https://www.cnblogs.com/loveyoume/p/6112044.html( ...
- CentOS7 64位下MySQL5.7安装与配置(YUM)
1.配置YUM源 在MySQL官网中下载YUM源rpm安装包:http://dev.mysql.com/downloads/repo/yum/ # 下载mysql源安装包 shell> wget ...
- iOS 枚举 初体验
iOS枚举 我的code /*文件名 SC_CDV_OCR.m*/ typedef enum _OCRResultState { OCRResultStateOK = 1, OCRResultStat ...
- hashChange & url change & QRCode & canvas to image
hashChange & url change & QRCode & canvas to image "use strict"; /** * * @auth ...
- monkeyrunner简介
monkeyrunner简介 MonkeyRunner工具是使用Jython(使用Java编程语言实现的Python)写出来的,它提供了多个API,通过monkeyrunner API 可以写一个Py ...
- codevs 2370 小机房的树(LCA)
过了这么长的时间终于开始看LCA了... 有一次训练题卡在LCA当时不会...拖了好久好久...其实现在还是不会... 只会tarjan... 传送门 板子题咯 tarjan的算法就是基于先序遍历的顺 ...
- C#中声明、调用和配置事件的演示源码
下面的内容是关于C#中声明.调用和配置事件的演示的内容,应该能对大伙有些好处. using System;namespace MyCollections { using System.Collecti ...
- luogu4365 秘密袭击 (生成函数+线段树合并+拉格朗日插值)
求所有可能联通块的第k大值的和,考虑枚举这个值: $ans=\sum\limits_{i=1}^{W}{i\sum\limits_{S}{[i是第K大]}}$ 设cnt[i]为连通块中值>=i的 ...
- GraphQL
GraphQL 官方描述: GraphQL 既是一种用于 API 的查询语言也是一个满足你数据查询的运行时. GraphQL 对你的 API 中的数据提供了一套易于理解的完整描述,使得客户端能够准确地 ...