437 Path Sum III 路径总和 III
给定一个二叉树,二叉树的每个节点含有一个整数。
找出路径和等于给定数的路径总数。
路径不需要从根节点开始,也不需要在叶节点结束,当路径方向必须是向下的(只从父节点到子节点)。
二叉树不超过1000个节点,节点的整数值的范围是[-1000000,1000000]。
示例:
root = [10,5,-3,3,2,null,11,3,-2,null,1], sum = 8
10
/ \
5 -3
/ \ \
3 2 11
/ \ \
3 -2 1
返回 3. 和等于8的路径有:
1. 5 -> 3
2. 5 -> 2 -> 1
3. -3 -> 11
详见:https://leetcode.com/problems/path-sum-iii/description/
C++:
方法一:
/**
* 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==nullptr)
{
return 0;
}
return helper(root,sum)+pathSum(root->left,sum)+pathSum(root->right,sum);
}
int helper(TreeNode *root,int sum)
{
int res=0;
if(root==nullptr)
{
return res;
}
if(sum==root->val)
{
++res;
}
res+=helper(root->left,sum-root->val);
res+=helper(root->right,sum-root->val);
return res;
}
};
方法二:
/**
* 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) {
int res=0;
vector<TreeNode*> out;
helper(root,sum,0,out,res);
return res;
}
void helper(TreeNode* node,int sum,int curSum,vector<TreeNode*> &out,int &res)
{
if(!node)
{
return;
}
out.push_back(node);
curSum+=node->val;
if(curSum==sum)
{
++res;
}
int t=curSum;
for(int i=0;i<out.size()-1;++i)
{
t-=out[i]->val;
if(t==sum)
{
++res;
}
}
helper(node->left,sum,curSum,out,res);
helper(node->right,sum,curSum,out,res);
out.pop_back();
}
};
参考:https://www.cnblogs.com/grandyang/p/6007336.html
437 Path Sum III 路径总和 III的更多相关文章
- 113 Path Sum II 路径总和 II
给定一个二叉树和一个和,找到所有从根到叶路径总和等于给定总和的路径.例如,给定下面的二叉树和 sum = 22, 5 / \ 4 ...
- LeetCode 113. Path Sum II路径总和 II (C++)
题目: Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the give ...
- Leetcode113. Path Sum II路径总和2
给定一个二叉树和一个目标和,找到所有从根节点到叶子节点路径总和等于给定目标和的路径. 说明: 叶子节点是指没有子节点的节点. 示例: 给定如下二叉树,以及目标和 sum = 22, 5 / \ 4 8 ...
- 【LeetCode】113. Path Sum II 路径总和 II 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.me/ 文章目录 题目描述 题目大意 解题方法 BFS DFS 日期 题目地址:https:// ...
- [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 ...
- 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. 路径总和 III
437. 路径总和 III 给定一个二叉树,它的每个结点都存放着一个整数值. 找出路径和等于给定数值的路径总数. 路径不需要从根节点开始,也不需要在叶子节点结束,但是路径方向必须是向下的(只能从父节点 ...
- Java实现 LeetCode 437 路径总和 III(三)
437. 路径总和 III 给定一个二叉树,它的每个结点都存放着一个整数值. 找出路径和等于给定数值的路径总数. 路径不需要从根节点开始,也不需要在叶子节点结束,但是路径方向必须是向下的(只能从父节点 ...
随机推荐
- Android中的图片查看器
本案例,使用Eclipse来开发Android2.1版本号的图片查看器. 1)首先,打开Eclipse.新建一个Android2.1版本号的项目ShowTu,打开res/values中文件夹下的str ...
- Redis 命令行 常用总结
http://www.redis.cn/commands.html# 1 Keys * 列出所有的keys redis > keys * ) "s:0" ) "o: ...
- Hive中行列转换
1.演示多列转为单行 数据文件及内容: student.txt xiaoming|english|92.0 xiaoming|chinese|98.0 xiaoming|math|89.5 huahu ...
- POJ 2545+2591+2247+1338简单水题
[题意简述]:就是有这种一个序列.就拿当p1 = 2,p2 = 3, p3 = 5,来举例.由这三个数为基准组成的序列是: 2,3,4,5,6,8,9,10,12--如今给你这个序列数组的下标,让你求 ...
- ios archives 出现的是other items而不是iOS Apps的解决方案
ios archives 出现的是other items而不是iOS Apps的解决方案 项目打包时出现的是不是出现在iOS Apps栏目下面,而是Other Items而且右边对应的Upload t ...
- CSVReader
从网上找了一个开源的东东 ,网址 https://www.csvreader.com/
- Tomcat 安装错误
安装tomcat时,遇到"failed to install tomcat6 service check your settings and permissions"的问题 安装时 ...
- browser user agent
乐视X501 UC浏览器1080x1920x32Mozilla/5.0 (Linux; U; Android 5.0.2; zh-CN; Letv X501 Build/DBXCNOP55013041 ...
- windows exe程序点击可以运行,但任务计划时程序不运行
问题描述:exe程序双击或者cmd执行都可以,但是配置了计划任务就一闪而过,并没有对应log产生. 可能会有和我同样的问题的小伙伴,这里记录一下. 解决方法:来在St ...
- Lightoj 1023 - Discovering Permutations
1023 - Discovering Permutations PDF (English) Statistics Forum Time Limit: 0.5 second(s) Memory L ...