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——给出一个数,从根到子的和等于它的更多相关文章

  1. Poj The xor-longest Path 经典题 Trie求n个数中任意两个异或最大值

    Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 5646   Accepted: 1226 Description In an ...

  2. Path Sum II - LeetCode

    目录 题目链接 注意点 解法 小结 题目链接 Path Sum II - LeetCode 注意点 不要访问空结点 解法 解法一:递归,DFS.每当DFS搜索到新节点时,都要保存该节点.而且每当找出一 ...

  3. Leetcode: mimimum depth of tree, path sum, path sum II

    思路: 简单搜索 总结: dfs 框架 1. 需要打印路径. 在 dfs 函数中假如 vector 变量, 不用 & 修饰的话就不需要 undo 2. 不需要打印路径, 可设置全局变量 ans ...

  4. [Leetcode Week14]Path Sum II

    Path Sum II 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/path-sum-ii/description/ Description Giv ...

  5. 【LeetCode】113. Path Sum II 解题报告(Python)

    [LeetCode]113. Path Sum II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fu ...

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

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

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

  9. javascript基础程序(算出一个数的平方值、算出一个数的阶乘、输出!- !- !- !- !- -! -! -! -! -! 、函数三个数中的最大数)

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

随机推荐

  1. 利用Python分析羊车门问题

    题目描述:有3扇关闭的门,一扇门后面停着汽车,其余门后是山羊,只有主持人知道每扇门后面是什么.参赛者可以选择一扇门,在开启它之前,主持人会开启另外一扇门,露出门后的山羊,然后允许参赛者更换自己的选择. ...

  2. 当网络中断的时候,JTA全局事务管理,究竟会不会回滚???

    前言:有人问了我一个问题,就是说在网络中断的时候,JTA的全局事务管理,会不会回滚?当时说会回滚,但没给对方说清楚理由,也不太认同我的观点.现在总结一下. 今天一天都在看文档(也查了一些博客和网站), ...

  3. .net SignalR 聊天室

    代码地址:https://gitee.com/srnsrn/netSignalr.git 运行项目打开多个页面 私密聊天

  4. [BZOJ3585][BZOJ3339]mex

    [BZOJ3585][BZOJ3339]mex 试题描述 有一个长度为n的数组{a1,a2,...,an}.m次询问,每次询问一个区间内最小没有出现过的自然数. 输入 第一行n,m.第二行为n个数.从 ...

  5. Django标签之包含标签Inclusion tags

    Django过滤器和标签功能很强大,而且支持自定义标签,很是方便:其中一种标签是Inclusion tags,即包含标签,个人感觉比较反人类的 包含标签(Inclusion tags)通过渲染其他的模 ...

  6. POJ3671 Dining Cows

    Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8126   Accepted: 3441 Description The c ...

  7. 介绍一款移动端滚动加载的插件---dropload

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. 论epoll的实现

    论epoll的实现 上一篇博客 论select的实现 里面已经说了为什么 select 比较慢.poll 的实现和 select 类似,只是少了最大 fd 限制,如果有兴趣可以自己去看代码.我这里来简 ...

  9. NYOJ90 整数划分(经典递归和dp)

    整数划分 时间限制:3000 ms  |  内存限制:65535 KB 难度:3   描述 将正整数n表示成一系列正整数之和:n=n1+n2+…+nk,  其中n1≥n2≥…≥nk≥1,k≥1.  正 ...

  10. (入门SpringBoot)SpringBoot结合redis(四)

    SpringBoot整合redis: 1.引入jar <!--  引入redis依赖 --><dependency>    <groupId>org.springf ...