112题目如下:

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 and sum
= 22
,

              5
/ \
4 8
/ / \
11 13 4
/ \ \
7 2 1

return true, as there exist a root-to-leaf path 5->4->11->2 which sum is 22.

113题目如下:

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 and sum
= 22
,

              5
/ \
4 8
/ / \
11 13 4
/ \ / \
7 2 5 1

return

[
[5,4,11,2],
[5,8,4,5]
]

112和113题目是类似的,都是找出等于给定值的路径,不过前者只看有没有,后者是要输出所有符合条件的路径。

112由于只要看有没有等于给定值的路径,所以可以用BFS,将每个树节点的val改为从根节点到当前节点的距离,这样改到树的叶子节点的时候,就可以判断是否有叶子节点的距离值符合要求。

113由于需要输出所有符合条件的路径,如果树节点的数据结构不可以改,那这就不适合用BFS,因为如果用BFS需要在每个树节点里维护一个到达当前节点经过的节点记录;所以这里改用DFS,通过修改一个存储路径的vector来统计所有满足条件的路径。

112题目代码

/**
* 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:
bool hasPathSum(TreeNode* root, int sum) {
queue<TreeNode*> q;
vector<TreeNode*> v;
if(root==NULL) return false;
q.push(root);
TreeNode* p=root;
while(q.size()!=0)
{
p=q.front();
q.pop();
if(p->left!=NULL)
{
p->left->val+=p->val;
q.push(p->left);
}
if(p->right!=NULL)
{
p->right->val+=p->val;
q.push(p->right);
}
if(p->left==NULL && p->right==NULL)
v.push_back(p);
}
for(int i=0;i<v.size();i++)
if(v[i]->val==sum) return true;
return false; }
};

113题目代码

/**
* 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:
//用来递归的函数
void findSum(TreeNode* t,vector<int>& temp, vector<vector<int> >& record, int sum, int cur)
{
temp.push_back(t->val);
cur+=t->val;
//在叶节点找到了目标值
if(cur==sum && t->left==NULL && t->right==NULL) {
record.push_back(temp);
temp.erase(temp.end()-1);
cur-=t->val;
return ;
}
//查找左右节点
if(t->left!=NULL) findSum(t->left, temp, record, sum , cur);
if(t->right!=NULL) findSum(t->right, temp, record, sum , cur);
//返回前更新临时路径的记录和累加和
temp.erase(temp.end()-1);
cur-=temp[temp.size()-1];
return;
}
vector<vector<int>> pathSum(TreeNode* root, int sum) {
vector<vector<int> > record;//记录最终要返回的路径
if(root==NULL) return record;
vector<int> temp;//记录当前临时路径
findSum(root,temp,record,sum,0);
return record;
}
};
												

Leetcode题 112 和 113. Path Sum I and II的更多相关文章

  1. 【LeetCode】112. 路径总和 Path Sum 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS 回溯 BFS 栈 日期 题目地址:https ...

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

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

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

  4. 【一天一道LeetCode】#113. Path Sum II

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  5. [LeetCode] 113. Path Sum II 路径和 II

    Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...

  6. LeetCode 112. 路径总和(Path Sum) 10

    112. 路径总和 112. Path Sum 题目描述 给定一个二叉树和一个目标和,判断该树中是否存在根节点到叶子节点的路径,这条路径上所有节点值相加等于目标和. 说明: 叶子节点是指没有子节点的节 ...

  7. [leetcode] 113. Path Sum II (Medium)

    原题链接 子母题 112 Path Sum 跟112多了一点就是保存路径 依然用dfs,多了两个vector保存路径 Runtime: 16 ms, faster than 16.09% of C++ ...

  8. [LeetCode] 113. Path Sum II 二叉树路径之和之二

    Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...

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

随机推荐

  1. 使用node建立本地服务器访问静态文件

    最终目录结构 demo │ node_modules └───public │ │ index.html │ │ index.css │ └───index.js └───server.js 一.使用 ...

  2. FreeRTOS 任务创建和删除(动态)

    TaskHandle_t taskhandle; TaskHandle_t taskhandle1; void vTask(void *t) { int i = 0; while(1) { i++; ...

  3. cookie和session以及iOS cookie的查取

    Cookie的工作原理 http是无状态的,这是什么意思呢?就是说,在没有cookie之前,你第一次访问这个页面和第二次访问这个页面, 服务器是不知道的,不知道前一次是你.那么问题来了,我怎么登录,登 ...

  4. Servlet快速入门:第一个Servlet程序

    Servlet是整个JavaWeb开发的核心,同时也是一套规范,即公共接口.用于处理客户端发来的请求并作出响应.通常情况下我们会发送不同的请求并交由不同的处理程序来处理,例如处理用户信息和处理订单信息 ...

  5. cuda环境搭建

    cuda环境搭建 cuda 的安装 一篇很不错的博客 https://blog.csdn.net/u014529295/article/details/78766258 另外官网也有介绍 https: ...

  6. Tensorflow&CNN:验证集预测与模型评价

    版权声明:本文为博主原创文章,转载 请注明出处:https://blog.csdn.net/sc2079/article/details/90480140 - 写在前面 本科毕业设计终于告一段落了.特 ...

  7. SpringCloud之Eureka

    [前面的话]SpringCloud为开发人员提供了快速构建分布式系统的一些工具,包括配置管理.服务发现.断路器.路由.微代理.事件总线.全局锁.决策竞选.分布式会话等等.它配置简单,上手快,而且生态成 ...

  8. 第七届蓝桥杯C/C++程序设计本科B组决赛 ——凑平方数(填空题)

    凑平方数 把0~9这10个数字,分成多个组,每个组恰好是一个平方数,这是能够办到的.比如:0, 36, 5948721 再比如:10985247361, 25, 63907840, 4, 289, 1 ...

  9. SpringBoot自动配置的魔法是怎么实现的

    SpringBoot 最重要的功能就是自动配置,帮我们省去繁琐重复地配置工作.相信用过SpringBoot的人,都会被它简洁的步骤所惊讶.那么 SpringBoot 是如何实现自动配置的呢? 在这之前 ...

  10. TOMCAT上传下载文件

    实现下载 修改server.xml修改web.xml   实现上传 实现客户端的上传post请求代码实现 实现服务端的处理   小结         实现下载 实现下载需要  - 修改Tomcat中的 ...