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. Centos 6.5 Apache服务安装

     Apache是什么: Apache HTTP Server(简称Apache)是Apache软件基金会的一个高性能.功能强大.稳定可靠.又很灵活的开发源代码的web服务软件,对linux的支持相对好 ...

  2. springcloud 之Ribbon客户端负载均衡配置使用

    pom.xml添加配置说明:这里服务注册与发现用的是Eureka,所以消费者端需要引入eureka,使用EurekaClient来调用服务 <dependency> <groupId ...

  3. 管理Linux软件——aptitude

    https://help.ubuntu.com/lts/serverguide/aptitude.html.en

  4. Python 写入训练日志文件并控制台输出

    1. 背景 在深度学习的任务中,通常需要比较长时间的训练,因此我们会选择离开电脑.笔者在跟踪模型表现, 观察模型accuracy 以及 loss 的时候,比较传统的方法是在控制台print输出或者直接 ...

  5. 拦截器(Interceptor)和过滤器(Filter)的执行顺序和区别

    一.引言 本来想记录一下关于用户登陆和登陆之后的权限管理.菜单管理的问题,想到解决这个问题用到Interceptor,但想到了Interceptor,就想到了Filter,于是就想说一下它们的执行顺序 ...

  6. python函数名的应用、闭包和迭代器

    一.函数名的应用(第一类对象) 函数名是一个变量,但它是一个特殊的变量,与括号配合可以执行函数变量. 1.函数名的内存地址 def func(): print("哈哈") prin ...

  7. subprocess模块笔记

    subprocess笔记 import subprocess subprocess.call("netstat -ano") #父进程等待子进程完成任务,返回执行结果和结束信息0或 ...

  8. el-input maxlength 不限制长度

    背景: 小鱼最近使用 input输入框时想限制输入的长度, type = "number" 时,限制的长度无效,代码如下 <el-input v-model="fo ...

  9. 应用程序域 System.AppDomain,动态加载程序集

    一.概述 使用.NET建立的可执行程序 *.exe,并没有直接承载到进程当中,而是承载到应用程序域(AppDomain)当中.在一个进程中可以包含多个应用程序域,一个应用程序域可以装载一个可执行程序( ...

  10. Mybatis之foreach批量插入

    1接口 public boolean insertMembersBatch(@Param("memberList") List<Members> members); @ ...