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. 小数据玩转Pyspark(2)

    一.客户画像 客户画像应用:精准营销(精准预测.个性化推荐.联合营销):风险管控(高风险用户识别.异常用户识别.高可疑交易识别):运营优化(快速决策.产品组合优化.舆情分析.服务升级):业务创新(批量 ...

  2. sql server split切割字符串

    create FUNCTION [dbo].[dnt_split] ( @splitstring varchar(max), @separator CHAR() = ',' ) RETURNS @sp ...

  3. OpenStack kilo版(4) Glance部署

    Glance简介 Glance-api:接受云系统镜像的构建.删除.读取请求 Glance-Registry:云系统的镜像注册服务 部署在controller节点 配置数据库 MariaDB [(no ...

  4. Android笔记(三十二) Android中线程之间的通信(四)主线程给子线程发送消息

    之前的例子都是我们在子线程(WorkerThread)当中处理并发送消息,然后在主线程(UI线程)中获取消息并修改UI,那么可以不可以在由主线程发送消息,子线程接收呢?我们按照之前的思路写一下代码: ...

  5. C++——static & const

    静态成员 由关键字static修饰说明的类成员,称为静态类成员(static class member).虽然使用static修饰说明,但与函数中的静态变量有明显差异.类的静态成员为其所有对象共享,不 ...

  6. Java基本知识点o(1), o(n), o(logn), o(nlogn)的了解

    在描述算法复杂度时,经常用到o(1), o(n), o(logn), o(nlogn)来表示对应算法的时间复杂度, 这里进行归纳一下它们代表的含义: 这是算法的时空复杂度的表示.不仅仅用于表示时间复杂 ...

  7. C#ThreadPool类—多线程

    标题:ThreadPool Class 地址:https://docs.microsoft.com/zh-cn/dotnet/api/system.threading.threadpool?redir ...

  8. shell中判断前一个命令是否执行成功

    ]; then echo "fail" else echo "success" fi 或者 ]; then echo "success" e ...

  9. Codeforces #364 (Div. 2) D. As Fa(数学公式推导 或者二分)

    数学推导的博客 http://codeforces.com/contest/701/problem/D  题目 推导的思路就是 : 让每个人乘车的时间相等 ,让每个人走路的时间相等. 在图上可以这么表 ...

  10. MySQL远程连接错误解决

    远程连接服务器的MySQL数据库,错误代码是1130,是由于无法给远程连接的用户权限的问题 解决方法: 本机登陆mysql后,将mysql数据库中的user表中的host项,从localhost改为% ...