Path SumI、II——给出一个数,从根到子的和等于它
I、Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.
For example:
Given the below binary tree andsum = 22,
5
/ \
4 8
/ / \
11 13 4
/ \ \
7 2 1
return true, as there exist a root-to-leaf path5->4->11->2which sum is 22.
PS:要想好判断条件,递归的时候考虑到必须要到子节点。
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
bool hasPathSum(TreeNode *root, int sum) {
if(root==NULL) return false;
return dfs(root,sum);
}
bool dfs(TreeNode *root, int sum){
if(root==NULL) return false;
if(root->left==NULL&&root->right==NULL&&sum==root->val) return true;
return dfs(root->left,sum-root->val)||dfs(root->right,sum-root->val);
}
};
II、
Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum.
For example:
Given the below binary tree andsum = 22,
5
/ \
4 8
/ / \
11 13 4
/ \ / \
7 2 5 1
return
[
[5,4,11,2],
[5,8,4,5]
]
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<vector<int> > pathSum(TreeNode *root, int sum) {
dfs(root,sum);
return res;
}
void dfs(TreeNode *root, int sum){
if(root==NULL) return;
v.push_back(root->val);
if(root->left==NULL&&root->right==NULL&&root->val==sum){
res.push_back(v);
}else{
dfs(root->left,sum-root->val);
dfs(root->right,sum-root->val);
}
v.pop_back();
}
vector<int> v;
vector<vector<int>> res;
};
Path SumI、II——给出一个数,从根到子的和等于它的更多相关文章
- Poj The xor-longest Path 经典题 Trie求n个数中任意两个异或最大值
Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 5646 Accepted: 1226 Description In an ...
- Path Sum II - LeetCode
目录 题目链接 注意点 解法 小结 题目链接 Path Sum II - LeetCode 注意点 不要访问空结点 解法 解法一:递归,DFS.每当DFS搜索到新节点时,都要保存该节点.而且每当找出一 ...
- Leetcode: mimimum depth of tree, path sum, path sum II
思路: 简单搜索 总结: dfs 框架 1. 需要打印路径. 在 dfs 函数中假如 vector 变量, 不用 & 修饰的话就不需要 undo 2. 不需要打印路径, 可设置全局变量 ans ...
- [Leetcode Week14]Path Sum II
Path Sum II 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/path-sum-ii/description/ Description Giv ...
- 【LeetCode】113. Path Sum II 解题报告(Python)
[LeetCode]113. Path Sum II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fu ...
- Leetcode 笔记 113 - Path Sum II
题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...
- Path Sum II
Path Sum II Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals ...
- [leetcode]Path Sum II
Path Sum II Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals ...
- javascript基础程序(算出一个数的平方值、算出一个数的阶乘、输出!- !- !- !- !- -! -! -! -! -! 、函数三个数中的最大数)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
随机推荐
- XeLaTeX插入GB/T 7714-2005规范的参考文献方法
GB/T 7714-2005 biblatex 在使用XeLaTeX的过程中,会遇到参考文献需要按照GB/T 7714-2005规范的情况.此时需要使用biblatex宏包,并且指定包的参数为 ...
- [git 学习篇]远程创库
实际情况往往是这样,找一台电脑充当服务器的角色,每天24小时开机,其他每个人都从这个“服务器”仓库克隆一份到自己的电脑上,并且各自把各自的提交推送到服务器仓库里,也从服务器仓库中拉取别人的提交. 完全 ...
- Educational Codeforces Round 34 (Rated for Div. 2)
A. Hungry Student Problem time limit per test 1 second memory limit per test 256 megabytes input sta ...
- http.server()的理解
http.server()相当于实例化一个server,等价于http.createServer(). 以下为个人理解 http.server()为创建一个http服务,http.server()可以 ...
- js多少时间之前
<?php $time = time()*1000; $end_time = strtotime("2018-01-01")*1000; $time_ago = $time ...
- 漫谈登录桩(MockStub)的实现
2014年6月4日,6月的第一个星期三,我正式入职百度,开始baiduer的工作.这不到2个月的时间,因为人力资源这边原因,我从INF部门离开,拉到了百度Hi-Server团队中来.2个完全不着调的岗 ...
- 【Luogu】P4035球形空间产生器(高斯消元)
题目链接 水比题,把圆方程展开减一下把平方都减掉半径的平方也减掉,高斯消元即可. 然后我只输出两位小数,爆了两次零.我好菜啊. #include<cstdio> #include<c ...
- HUST——1106xor的难题之二(异或树状数组单点修改和区间查询)
1106: xor的难题之二 时间限制: 2 Sec 内存限制: 128 MB 提交: 8 解决: 3 题目描述 上次Alex学长的问题xor难题很简单吧,现在hkhv学长有个问题想问你们. 他现 ...
- BZOJ1499 [NOI2005]瑰丽华尔兹 【单调队列优化dp】
题目 你跳过华尔兹吗?当音乐响起,当你随着旋律滑动舞步,是不是有一种漫步仙境的惬意?众所周知,跳华尔兹时,最重要的是有好的音乐.但是很少有几个人知道,世界上最伟大的钢琴家一生都漂泊在大海上,他的名字叫 ...
- 刷题总结——math(NOIP模拟)
题目: 给定两个数字n,求有多少个数字b满足a^b和b^a同余于2^n,其中n<=30,a<=10^9, 题解: 挺巧妙的一道题···从中深深体会到打表的重要性··· 首先根据ab奇偶性分 ...