Leetcode题 112 和 113. Path Sum I and II
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的更多相关文章
- 【LeetCode】112. 路径总和 Path Sum 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS 回溯 BFS 栈 日期 题目地址:https ...
- 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 ...
- 【LeetCode】113. Path Sum II 解题报告(Python)
[LeetCode]113. Path Sum II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fu ...
- 【一天一道LeetCode】#113. Path Sum II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- [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 ...
- LeetCode 112. 路径总和(Path Sum) 10
112. 路径总和 112. Path Sum 题目描述 给定一个二叉树和一个目标和,判断该树中是否存在根节点到叶子节点的路径,这条路径上所有节点值相加等于目标和. 说明: 叶子节点是指没有子节点的节 ...
- [leetcode] 113. Path Sum II (Medium)
原题链接 子母题 112 Path Sum 跟112多了一点就是保存路径 依然用dfs,多了两个vector保存路径 Runtime: 16 ms, faster than 16.09% of C++ ...
- [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 ...
- 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 ...
随机推荐
- select加锁分析(Mysql)
[原创]惊!史上最全的select加锁分析(Mysql) 前言 大家在面试中有没遇到面试官问你下面六句Sql的区别呢 select * from table where id = ? select * ...
- Redis 知识 整理
简介 安装 启动 注意事项 使用命令 通用命令 数据结构 字符串(string) 哈希(hash) 队列(list) 集合(set) 有序集合(zset) 位图(bitcount) 事务 订阅与发布 ...
- Ubuntu18.04开机启动sudo命令
首先接前文:ubuntu18.04 下启动Android Studio报错KVM is required to run this AVD. /dev/kvm device: permission de ...
- 前端HTML基础和head部分
一.SOCKET服务器与浏览器交互 CS模式 --> BS模式 CS模式逐渐向BS模式转移,底层都是socket客户端 浏览器给服务器发送请求 --> 服务器收到请求 --> 服务 ...
- FDD-LTE上下行带宽一样的,为什么上下行流量差别这么大
转:https://zhidao.baidu.com/question/923940070377297579.html 虽然FD系统,上下行使用的带宽一样,但是上下行的信号编码效率完全不同.上行信号( ...
- VSCode安装程序——java开发
文章:微软为 Java 开发者推出 VSCode 安装程序 文章介绍微软为VSCode提供了开发程序,方便java开发者更好的使用VSCode
- Codeforces Round 585
Codeforces Round 585 浅论如何发现自己是傻子的-- 反正今天是完全蒙的,水了签到题就跑了-- A. Yellow Cards 签到题. 众所周知,CF的签到题一般是一道神神奇奇的数 ...
- P1273 有线电视网[分组背包+树形dp]
题目描述 某收费有线电视网计划转播一场重要的足球比赛.他们的转播网和用户终端构成一棵树状结构,这棵树的根结点位于足球比赛的现场,树叶为各个用户终端,其他中转站为该树的内部节点. 从转播站到转播站以及从 ...
- 预定义的基础类型转换,BitConverter,BitArray
一.BitConverter 将预定义的基础类型与字节数据进行互转 1.将值类型转成字节数组(Unicode):BitConverter.GetBytes() byte[] data = BitCon ...
- H5--性能测试(PageSpeed Insights )
中文网站(不需要翻墙): http://www.googlespeed.cn 主要可实现: 1.测试手机与电脑打开的速度对比. 2.详细的测试结果 3.直观的统计数据,查看页面的共xx个请求.总共大小 ...