这道题是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(路径总和)的更多相关文章

  1. 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 ...

  2. 112 Path Sum 路径总和

    给定一棵二叉树和一个总和,确定该树中是否存在根到叶的路径,这条路径的所有值相加等于给定的总和.例如:给定下面的二叉树和 总和 = 22,              5             / \  ...

  3. [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 ...

  4. [leetcode] Path sum路径之和

    要求给定树,与路径和,判断是否存在从跟到叶子之和为给定值的路径.比如下图中,给定路径之和为22,存在路径<5,4,11,2>,因此返回true;否则返回false. 5 / \ 4 8 / ...

  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 ...

  6. 【LeetCode】112. 路径总和 Path Sum 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS 回溯 BFS 栈 日期 题目地址:https ...

  7. [LeetCode] Path Sum III 二叉树的路径和之三

    You are given a binary tree in which each node contains an integer value. Find the number of paths t ...

  8. [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 ...

  9. [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 ...

随机推荐

  1. 【bzoj3033】太鼓达人

    3033: 太鼓达人 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 521  Solved: 399[Submit][Status][Discuss] ...

  2. 转 如何快速清理 chrom 缓存

    谷歌浏览器(Chrome)如何手动清除缓存 听语音 | 浏览:13267 | 更新:2014-05-15 01:00 | 标签:谷歌 chrome 浏览器的缓存可以帮助我们更好地使用一些程序,但时间长 ...

  3. Thinking In Java持有对象阅读记录

    这里记录下一些之前不太了解的知识点,还有一些小细节吧 序 首先,为什么要有Containers来持有对象,直接用array不好吗?——数组是固定大小的,使用不方便,而且是只能持有一个类型的对象,但当你 ...

  4. Django 使用allauth报错

    一:报错 RuntimeError: Model class django.contrib.sites.models.Site doesn't declare an explicit app_labe ...

  5. JSP新闻发布系统

    1.主页面 1.1登录 1.2    分页 2.注销 3.代码如下 package cn.news.dao.impl; import java.sql.SQLException; import org ...

  6. js js弹出框、对话框、提示框、弹窗总结

    js弹出框.对话框.提示框.弹窗总结 一.JS的三种最常见的对话框 //====================== JS最常用三种弹出对话框 ======================== //弹 ...

  7. spring boot & mybatis集合的坑

    因为是使用的mybatis逆向工程自动生成的实体类和dao层,然后在读取某一个表的content字段时出现问题. 问题描述:在mysql数据库里可以直接查询到这个字段的内容,但是使用java相关的方法 ...

  8. Incredibuild导入key的方式

    作者:朱金灿 来源:http://blog.csdn.net/clever101 Incredibuild5.0采用新的授权机制,在安装完服务端之后右键单击它的授权文件License .ib_lic, ...

  9. java 删除字符串最后一个字符的几种方法

    偶然看到的,记录一下,以免忘记 字符串:string s = "1,2,3,4,5," 目标:删除最后一个 "," 方法:    1.用的最多的是Substri ...

  10. 为 Azure 应用服务配置连续部署工作流

    本快速入门介绍了如何将应用服务 GitHub 集成以实现连续部署工作流.在本教程中完成的所有操作均符合1元试用条件. 本快速入门介绍了如何将应用服务 GitHub 集成以实现连续部署工作流.在本教程中 ...