[LeetCode 112 113] - 路径和I & II (Path Sum I & II)
问题
给出一棵二叉树及一个和值,检查该树是否存在一条根到叶子的路径,该路径经过的所有节点值的和等于给出的和值。
例如,
给出以下二叉树及和值22:
5
/ \
4 8
/ / \
11 13 4
/ \ \
7 2 1
函数返回true,因为存在一条根到叶子的路径5->4->11->2,其路径和为22。
初始思路
鉴于题目要求找到一条路径和符合要求即可,选择层次遍历二叉树是一种比较合适的选择-保证了我们首先找到的是最短的路径从而节省了时间。另外由于没有禁止修改原二叉树的值,我们在处理过程中可以把每个点的值修改为到达这点时的路径和,方便比较。至于层次遍历的方法,大家应该已经很熟悉了,使用116,117中的双vector法即可:
class Solution {
public:
bool hasPathSum(TreeNode *root, int sum)
{
if(!root)
{
return false;
}
treeLevel_[].clear();
treeLevel_[].clear();
bool flag = false;
treeLevel_[].push_back(root);
while(!treeLevel_[flag].empty())
{
for(auto iter = treeLevel_[flag].begin(); iter != treeLevel_[flag].end(); ++iter)
{
if(!(*iter)->left && !(*iter)->right)
{
if((*iter)->val == sum)
{
return true;
}
}
if((*iter)->left)
{
(*iter)->left->val += (*iter)->val;
treeLevel_[!flag].push_back((*iter)->left);
}
if((*iter)->right)
{
(*iter)->right->val += (*iter)->val;
treeLevel_[!flag].push_back((*iter)->right);
}
}
treeLevel_[flag].clear();
flag = !flag;
}
return false;
}
private:
std::vector<TreeNode*> treeLevel_[];
};
hasPathSum
提交后Judge Small和Judge Large双双通过。
扩展问题
给出一棵二叉树及一个和值,找出符合条件的所有根到叶子的路径,这些路径经过的所有节点值的和等于给出的和值。
例如,
给出以下二叉树及和值22:
5
/ \
4 8
/ / \
11 13 4
/ \ / \
7 2 5 1
返回
[
[5,4,11,2],
[5,8,4,5]
]
扩展问题初始思路
题目要求找出所有符合条件的路径,意味着不管用什么方法都必须把所有节点遍历一遍。如果还是使用前面的层次遍历,因为是广度优先遍历,需要同时跟踪多条路径的和,直到走到叶子节点。因此在这里我们尝试使用普通的递归中序遍历,由于这是一种深度优先遍历,通过进栈及出栈操作,可以做到一次只需跟踪一条路径即可:
class Solution113 {
public:
std::vector<std::vector<int> > pathSum(TreeNode *root, int sum)
{
result_.clear();
if(!root)
{
return result_;
}
currentPath_.clear();
currentPath_.push_back(root->val);
sum_ = sum;
currentSum_ = root->val;
FindPathSum(root);
return result_;
}
private:
void FindPathSum(TreeNode *node)
{
if(!node->left && !node->right)
{
if(currentSum_ == sum_)
{
result_.push_back(currentPath_);
}
return;
}
if(node->left)
{
currentSum_ += node->left->val;
currentPath_.push_back(node->left->val);
FindPathSum(node->left);
currentSum_ -= node->left->val;
currentPath_.pop_back();
}
if(node->right)
{
currentSum_ += node->right->val;
currentPath_.push_back(node->right->val);
FindPathSum(node->right);
currentSum_ -= node->right->val;
currentPath_.pop_back();
}
}
std::vector<std::vector<int> > result_;
std::vector<int> currentPath_;
int sum_;
int currentSum_;
};
pathSum
提交后同样Judge Small和Judge Large双双通过。
[LeetCode 112 113] - 路径和I & II (Path Sum I & II)的更多相关文章
- Leetcode之动态规划(DP)专题-64. 最小路径和(Minimum Path Sum)
Leetcode之动态规划(DP)专题-64. 最小路径和(Minimum Path Sum) 给定一个包含非负整数的 m x n 网格,请找出一条从左上角到右下角的路径,使得路径上的数字总和为最小. ...
- [LeetCode] #112 #113 #437 Path Sum Series
首先要说明二叉树的问题就是用递归来做,基本没有其他方法,因为这数据结构基本只能用递归遍历,不要把事情想复杂了. #112 Path Sum 原题链接:https://leetcode.com/prob ...
- 【LeetCode】113. 路径总和 II
题目 给定一个二叉树和一个目标和,找到所有从根节点到叶子节点路径总和等于给定目标和的路径. 说明: 叶子节点是指没有子节点的节点. 示例: 给定如下二叉树,以及目标和 sum = 22, 5 / \ ...
- LeetCode:Path Sum I II
LeetCode:Path Sum Given a binary tree and a sum, determine if the tree has a root-to-leaf path such ...
- [Leetcode][JAVA] Path Sum I && II
Path Sum Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that addi ...
- leetcode -day17 Path Sum I II & Flatten Binary Tree to Linked List & Minimum Depth of Binary Tree
1. Path Sum Given a binary tree and a sum, determine if the tree has a root-to-leaf path such tha ...
- LeetCode(124) Binary Tree Maximum Path Sum
题目 Given a binary tree, find the maximum path sum. For this problem, a path is defined as any sequen ...
- Path Sum I && II & III
Path Sum I Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that ad ...
- Path Sum I&&II
I Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up ...
随机推荐
- KVM几种缓存模式
原文在这里: http://pic.dhe.ibm.com/infocenter/lnxinfo/v3r0m0/index.jsp?topic=%2Fliaat%2Fliaatbpkvmguestca ...
- centos 6.2 关闭 IPV6
在现在的Linux上IPv6已经在默认安装下被支持,但是对于一些对IPv6支持不是很好的应用服务器来说,开启了IPv6反而会影响服务器的网络性能,毕竟现在的网络交换设备不是IPv6的. 如何判断系统是 ...
- [原创]python MySQLdb在windows环境下的安装、出错问题以及解决办法
版权声明:本文为博主原创文章,未经博主允许不得转载. 问题:windows下安装MySQLdb的方法 解析:python没有php那种集成环境,比如wamp那种集成软件直接把所有需要的东西全部一次性搭 ...
- [置顶] ios 水果连连看游戏源码
原创文章,转载请注明出处:http://blog.csdn.net/donny_zhang/article/details/9251917 demo功能:水果连连看游戏源码.iphone6.1 测试通 ...
- 转载:c++内存泄露机制
对于一个c/c++程序猿来说,内存泄漏是一个常见的也是令人头疼的问题.已经有很多技术被研究出来以应对这个问题,比方 Smart Pointer,Garbage Collection等.Smart Po ...
- C primer plus 读书笔记第九章
本章的标题是函数.C的设计原则是把函数作为程序的构成模块. 1.函数概述 函数的定义:函数是用于完成特定任务的程序代码的自包含单元. 使用函数的原因:1.函数的使用可以省去重复代码的编写.2.使得程序 ...
- 二分-hdu-4768-Flyer
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4768 题目意思: 有n个A.B.C,每个Ai,Bi,Ci,对于每个P=Ai+k*Ci(P<=B ...
- yii CGridView colum 链接
默认显示的内容是没有链接的,现在想加链接,效果图如下 代码位置就在columns数组里,直接上代码说明 $this->widget('zii.widgets.grid.CGridView', a ...
- HDU 5139 Formula 卡内存
题目就是求这个 n达到10^7,测试数据组数为10^5 为了防止TLE,一开始把每个n对应的值先求出来,但发现竟然开不了10^7的数组(MLE),然后就意识到这是第一道卡内存的题目... 只能离线做, ...
- PHP博客小项目之知识点(1)
一.博客系统介绍 Blog.Bloger.web log(网络日志) 博客主要发布一些文章.图片:博客一般都是个人博客: 博客的文章,一般都是按照时间倒序排列: 博客,仅音译,英文名为Blogger, ...