【LeetCode】Path Sum(路径总和)
这道题是LeetCode里的第112道题。是我在学数据结构——二叉树的时候碰见的题。
题目要求:
给定一个二叉树和一个目标和,判断该树中是否存在根节点到叶子节点的路径,这条路径上所有节点值相加等于目标和。
说明: 叶子节点是指没有子节点的节点。
示例: 给定如下二叉树,以及目标和 sum = 22,
5
/ \
4 8
/ / \
11 13 4
/ \ \
7 2 1返回
true, 因为存在目标和为 22 的根节点到叶子节点的路径5->4->11->2。
本来是一道简单的题目,却因为开头的思路不正确,或者说是没有抓住重点,认真审题,导致提交过程中屡次碰壁。
下面给出我之前的思路:
起初是为了方便使用递归,然后递归参数使用当前节点的左右子节点,和sum值减去该节点的值。具体如下:
/**
* 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 isroot=;//这是拿来标记根节点的
bool hasPathSum(TreeNode* root, int sum) {
if (isroot) {
isroot = ;
if(root == NULL) return false;
if(root->left == NULL && root->right == NULL && sum == root->val) return true;
else return false;
}
if (root == NULL && sum == ) return true;
if (root == NULL && sum != ) return false;
return hasPathSum(root->left, sum - root->val) ||
hasPathSum(root->right, sum - root->val);
}
};
修改多次,每次上传都无法通过。然后没办法调到自己的编译器中调试,最后发现了问题,最大的问题就是题目要求的是从根节点到叶子节点的路径,而我这个算法是不能保证最后的节点是否是叶子节点。
发现了问题自然需要解决问题,然后我重写了一个算法,反而还更简短了:
/**
* 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) {
if (root == NULL)
return false;
sum = sum - root->val;
if (root->left == NULL && root->right == NULL && sum == )
return true;
return hasPathSum(root->left, sum) ||
hasPathSum(root->right, sum);
}
};
贴个结果:

【LeetCode】Path Sum(路径总和)的更多相关文章
- LeetCode 112. Path Sum路径总和 (C++)
题目: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up ...
- 112 Path Sum 路径总和
给定一棵二叉树和一个总和,确定该树中是否存在根到叶的路径,这条路径的所有值相加等于给定的总和.例如:给定下面的二叉树和 总和 = 22, 5 / \ ...
- [Leetcode] Path Sum路径和
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...
- [leetcode] Path sum路径之和
要求给定树,与路径和,判断是否存在从跟到叶子之和为给定值的路径.比如下图中,给定路径之和为22,存在路径<5,4,11,2>,因此返回true;否则返回false. 5 / \ 4 8 / ...
- 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】112. 路径总和 Path Sum 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS 回溯 BFS 栈 日期 题目地址:https ...
- [LeetCode] Path Sum III 二叉树的路径和之三
You are given a binary tree in which each node contains an integer value. Find the number of paths t ...
- [LeetCode] 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] Path Sum 二叉树的路径和
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...
随机推荐
- Ubuntu安装指定版本的docker
系统环境: Ubuntu 16.0.4 安装版本: docker 17.03.2 在进现在这家公司初期,需要使用rancher部署一个k8s集群,由于rancher也是由docker启动的,加上k8 ...
- 17995 Stupid thief 组合数学
17995 Stupid thief 时间限制:1000MS 内存限制:65535K提交次数:0 通过次数:0 题型: 编程题 语言: 不限定 Description A stupid thie ...
- python flask学习(2)
本文主要整理下几个Flask扩展: 0.Flask-Bootstrap:集成Twitter开发的一个开源框架Bootstrap.1.Flask-Script:为Flask程序添加一个命令行解析器2.F ...
- mybatis实现使用原生的sql
1.相应的xml文件中 <select id="selectByCategories" resultType="map" parameterType=&q ...
- Xcode 升级后,cocoaPod 问题
当我从Xcode 6.3切换到Xcode6.4的时候,因为我是mac上安装了两个不同的版本,现在把Xcode 6.3卸掉了. 现在再次运行pod install命令的时候,提示如下错误: Upda ...
- SQLServer 错误: 15404,无法获取有关 Windows NT 组/ 用户 'WIN-8IVSNAQS8T7\Administrator' 的信息,错误代码 0x534。
在自动清理日志的作业中,执行过程出现如下问题:“SQLServer 错误: 15404,无法获取有关 Windows NT 组/ 用户 'WIN-8IVSNAQS8T7\Administrator' ...
- HDU 1520 Anniversary party (树形DP,入门)
题意:给一棵树,每个节点都有权值,要求选择部分节点出来,使得权值之和最大,但是每对(父亲,儿子)中最多只能挑一个. 思路: 比较入门的题,每个节点可以选也可以不选.若当前节点选的话,孩子必须全部不选: ...
- codevs 2919 选择题
时间限制: 1 s 空间限制: 16000 KB 题目等级 : 黄金 Gold 题目描述 Description 某同学考试,在N*M的答题卡上写了A,B,C,D四种答案. 他做完了,又不能交,一 ...
- 1.JOIN和UNION区别
1.JOIN和UNION区别join 是两张表做交连后里面条件相同的部分记录产生一个记录集,union是产生的两个记录集(字段要一样的)并在一起,成为一个新的记录集 . JOIN用于按照ON条件联接两 ...
- iOS的设计备忘录/资源集合(新手快速开发)
iOS的设计备忘录 随着iOS7更新,风格走上扁平化,大部分iOS设计师及程序员都需要对自己的软件做相关调整,尺寸.Icon.UI等等,我在这里总结一下相关资料,以及提供一些关于iOS7设计素材. 一 ...