【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 ...
随机推荐
- 关于controller返回的页面js文件和css文件404问题的正式解决
谨用此博客记录一下这条卡了两个星期的bug…… 还是之前的问题,通过get方法,后台@Controller返回页面然后弹窗.但是不知道为什么一直所有js文件和css文件都报404…… (之前的博客记录 ...
- Less学习(2)(完结)
七.模式匹配与Guard表达式 根据传入参数的不同,引入不同的属性集. .mixin (dark, @color) { color: darken(@color, 10%); } .mixin (li ...
- 生产环境中mysql+keepalive双主模式,keepalive守护进程实现双主切换提供数据库服务
mysql+keepalive实现浮动地址自动切换,由于keepalive无自带健康检查功能,所以必须自动编写健康检查守护进程(监控DB1和DB2数据库的监控状态,来保证浮动地址双机自动切换.) 一, ...
- web移动端滑动插件
1.slip只有6.3k可以说是非常小了,主要是通过css3里面的transform来改变的位置,控制的是父容器,使用也非常简单,具体信息移步slip.js.一个简单的demo如下 <!DOCT ...
- 对象(Object)和类(Class)的关系?
对象属于某一类,即对象是某一个类的实例.例如: Public Class Flight Private _name As String Public Property Name As String G ...
- 对javascript变量提升跟函数提升的理解
在写javascript代码的时候,经常会碰到一些奇怪的问题,例如: console.log(typeof hello); var hello = 123;//变量 function hello(){ ...
- UITabBarController、导航控制器、控制器关系
UITabBarController与UINavigationController类似,UITabBarController也可以用来控制多个页面导航,用户可以在多个视图控制器之间移动,并可以定制屏幕 ...
- setuid
-r-s--x--x #s就是setuid,仅可用在二进制文件,对目录设置无效
- Java文件操作系列[1]——PDFBox实现分页提取PDF文本
需求:用java分页提取PDF文本. PDFBox是一个很好的可以满足上述需求的开源工具. 1.PDF文档结构 要解析PDF文本,我们首先要了解PDF文件的结构. 关于PDF文档,最重要的几点: 一, ...
- Node.js 打造实时多人游戏框架
在 Node.js 如火如荼发展的今天,我们已经可以用它来做各种各样的事情.前段时间UP主参加了极客松活动,在这次活动中我们意在做出一款让“低头族”能够更多交流的游戏,核心功能便是 Lan Party ...