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. day05_05 for循环、break语句

    1.0 输入用户名,密码练习 _user = "alex" _passwd = "abc123" username = input("Username ...

  2. 自己的Qt GUI 项目+vs2013+opencv+caffe环境配置

    由于深度学习的种种优势,使我们对于深度学习的使用越来越频繁.很多时候,我们都需要在自己的项目中配置caffe环境,来调用caffe网络模型完成自己的任务.今天我主要讲的关于"在自己的项目中配 ...

  3. 【Luogu】P3396哈希冲突(根号算法)

    题目链接 根号算法真的是博大精深啊……明明是暴力但复杂度就是能过 这也太强了吧!!! 预处理出p<=sqrt(n)的所有情况,耗时n根n 查询: 如果p<=根n,O1查表 如果p>= ...

  4. NOJ——1274The battle of Red Cliff(并查集按秩合并)

    [1274] The battle of Red Cliff 时间限制: 1000 ms 内存限制: 65535 K 问题描述 Zero loves palying Sanguo game very ...

  5. [SDOI2013]直径 (树的直径,贪心)

    题目链接 Solution 我们直接找到一条直径 \(s\),起点为 \(begin\),终点为 \(end\). 从前往后遍历点 \(u\) ,若子树中最大的距离与 \(dis(u,begin)\) ...

  6. 文本生成器(bzoj 1030)

    Description JSOI交给队员ZYX一个任务,编制一个称之为“文本生成器”的电脑软件:该软件的使用者是一些低幼人群,他们现在使用的是GW文本生成器v6版.该软件可以随机生成一些文章―――总是 ...

  7. Linux System Programming 学习笔记(十一) 时间

    1. 内核提供三种不同的方式来记录时间 Wall time (or real time):actual time and date in the real world Process time:the ...

  8. Linux System Programming 学习笔记(五) 进程管理

    1. 进程是unix系统中两个最重要的基础抽象之一(另一个是文件) A process is a running program A thread is the unit of activity in ...

  9. C# 打印webBrowser打开的页面

    this.webBrowser.Navigate(webBrowserUrl, tagerFrameName, postBuffer, heads); this.webBrowser.Document ...

  10. excel 表格lookup 的操作

    这个实现语句如下,(写在2行然后向下拖动)(2007或以上版本): = IF(,ISBLANK($H2),$G2="), "", IF("), IF($H2&g ...