这道题是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. POJ1129(贪心)

    笔者休息娱乐.贪心即可,爱怎么暴力怎么暴力.莽WA,改了几下算法发现是输出中二了-- int T, color[26], tot; bool adj[26][26]; string s; void g ...

  2. python flask学习(3)

    这次主要学习web表单.学了下,很像是Django的form表单验证.不过有许多的不同.可以说是功能更加碎块化.Django的验证方式是很固定和严谨的,风格完全不同. 尽管Flask的请求对象提供的对 ...

  3. 单线程单元(STA)线程都应使用泵式等待基元

    CLR 无法从 COM 上下文 0x20ad98 转换为 COM 上下文 0x20af08,这种状态已持续 60 秒.拥有目标上下文/单元的线程很有可能执行的是非泵式等待或者在不发送 Windows ...

  4. 后端 node 项目工具集

    后端 node 项目工具集 editor vs code webstorm 质量检查 eslint prettier 命令行相关 better-run-npm npm-run-all nodemon ...

  5. .aspx IIS发布404.17时候的问题

    .aspx IIS发布404.17时候的问题 在发布aspx的时候遇到问题利用网上的cmd加入注册表没有效果 在加入了MIME中加入.aspx和application/.aspx之后却变成了下载文件 ...

  6. 关于React的赋值与调用方法

    #关于React的赋值与调用方法 比如调用方法的时候我们可以这样来使用closeFrm() <div className = "infoFrm_close" onMouseO ...

  7. ES-Mac OS环境搭建(1)

    前言 由于elasticsearch依赖Java,所以先要配置上Java环境,并且Java JDK必须要求1.8以上,这里以安装Java 1.8为例.安装环境如下: elasticsearch6.5. ...

  8. centos 7下Hadoop 2.7.2 伪分布式安装

    centos 7 下Hadoop 2.7.2 伪分布式安装,安装jdk,免密匙登录,配置mapreduce,配置YARN.详细步骤如下: 1.0 安装JDK 1.1 查看是否安装了openjdk [l ...

  9. [Ubuntu]清除系统磁盘垃圾

    操作步骤: 1.sudo apt-get autoremove(卸载系统中所有未被使用的依赖关系) 2.sudo apt-get clean(清除所有缓存的包文件) 以上操作绿色无害,对系统无影响.

  10. IOS7适配

    (1)如果应用程序始终隐藏 status bar 那么恭喜呢,你在UI上需要的改动很少很少. (2)如果应用程序显示status bar,可以讲status bar设置成黑色不透明 ,然后在UIVie ...