【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 ...
随机推荐
- Photoshop给河边婚片加上唯美的霞光
<点小图查看大图> 最终效果 1.打开原图素材大图,按Ctrl + Alt + 2 调出高光选区,按Ctrl + Shift + I 反选得到暗部选区,然后创建曲线调整图层,对RGB.红. ...
- (四)jdk8学习心得之函数式接口
四.函数式接口 1. 格式 注:抽象方法就是通过lambda表达式或者方法引用实现. 2. Jdk提供的函数式接口(这里提供五个最为常用的) 3. 技巧 通过函数式接口,就可以把一个函数作为一个参数进 ...
- Linux下Nginx配置阿里云 SSL证书实现HTTPS访问
这篇文章主要介绍了nginx配置ssl证书实现https访问的示例 1.服务器系统:Centos 2. 阿里云申请SSL证书 选择“免费版DV SSL”,点击立即购买: 下载证书 列表中找到已签发的证 ...
- dataTables 插件学习整理
在项目中使用了dataTables 插件,学习整理一下. dataTables 的官方中文网站 http://www.datatables.club 引入文件: 所有的都要引入 jq文件 1. dat ...
- JPA javax.persistence.TransactionRequiredException
直接说一下解决方案 Dao层,一定要是Dao层. 1 增加Transactional,必须要事务! 2 增加Modifying,告诉jpa这是修改! @Transactional @Modifying ...
- 改善python程序的建议[转]
<编写高质量代码 改善Python程序的91个建议> <编写高质量代码 改善Python程序的91个建议>读后程序学习小结 - BigDeng_2014的专栏 - CSDN博客 ...
- 洛谷P1197 [JSOI2008]星球大战
题目 由于题目不要求强制在线,所以可以离线. 而离线的话就会带来许多便利,所以我们可以先处理出全部打击后的图,通过并查集来判断是否连通. 然后再从后往前枚举,得出答案 #include <bit ...
- BZOJ 2730 矿场搭建
割点 割点以外的点坍塌不影响其他人逃生,因为假设我们任取两个个非割点s建立救援站,非割点的任意点坍塌,我们都可以从割点走到一个救援出口. 所以我们只考虑割点坍塌的情况. 我们可以先找出图中所有的割点. ...
- 《深入理解java虚拟机》学习笔记-----郑雨迪
一.学习java虚拟机的原因 二.java代码的运行 java -c 编译成.class 文件(java字节码) java字节码(.class文件)无法直接执行,因此,java虚拟机需要将字节码编译成 ...
- Linux下批量管理工具pssh安装和使用
Linux下批量管理工具pssh安装和使用 pssh工具包 安装:yum -y install pssh pssh:在多个主机上并行地运行命令 pscp:把文件并行地复制到多个主机上 prsync:通 ...