/**
* Definition for binary tree
* 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==) //针对根结点,非根节点不应会执行到这里
return false; if( root->left== && root->right== ) //是叶子结点,且刚好将sum变为0
return root->val==sum ? true : false ; if( ( root->left!= && hasPathSum( root->left , sum-root->val) ) || ( root->right!= && hasPathSum( root->right , sum-root->val ) ) )//判断左叉或右叉中是否有一条从上往下满足要求的路径
return true; return false;
}
};

这次终于感觉代码够简洁的了。哈哈

题意是:有没有一条这样一条路径,从根开始到叶子结点上的值之和为所提供的数字。

  本来挺不愿意用递归的,递归很有局限性,但用起来又特别爽。像此题,想半个小时没想到怎么设计算法会快一点。如果有非递归算法,且是较好的代码,请不吝分享一下吧!

解题需考虑的是:

1.根结点为空

2.递归到叶子结点了,要设计其作为递归出口

3.非叶子结点要解决sum的问题。只要左子树或者右子树中有一条路径满足要求,那么就判断结束。

LeetCode Path Sum 判断树的路径之和的更多相关文章

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

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

  2. [LeetCode] Path Sum IV 二叉树的路径和之四

    If the depth of a tree is smaller than 5, then this tree can be represented by a list of three-digit ...

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

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

  5. [leetcode] Path sum路径之和

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

  6. [LeetCode] 666. Path Sum IV 二叉树的路径和 IV

    If the depth of a tree is smaller than 5, then this tree can be represented by a list of three-digit ...

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

  8. [LeetCode] 113. Path Sum II ☆☆☆(二叉树所有路径和等于给定的数)

    LeetCode 二叉树路径问题 Path SUM(①②③)总结 Path Sum II leetcode java 描述 Given a binary tree and a sum, find al ...

  9. [Leetcode] Binary tree maximum path sum求二叉树最大路径和

    Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. ...

随机推荐

  1. Luogu P2107 小Z的AK计划 堆贪心

    好久不做这种题了... 存一下每个点的位置和时间,由于达到某个位置跟之前去哪里AK的无关,所以在时间超限后,可以用大根堆弹掉之前消耗时间最大的,来更新答案,相当于去掉之前花费最大的,直到时间不在超限. ...

  2. BigDecimal取整

    Java中BigDecimal取整方法 BigDecimal bd = new BigDecimal("12.1"); long l = bd.setScale( 0, BigDe ...

  3. Linux内核硬件访问技术

    ① 驱动程序控制设备,主要是通过访问设备内的寄存器来达到控制目的.因此我们讨论如何访问硬件,就成了如何访问这些寄存器. ② 在Linux系统中,无论是内核程序还是应用程序,都只能使用虚拟地址,而芯片手 ...

  4. 通过JS,按照原比例控制图片尺寸

    <html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Con ...

  5. docker(4)使用Dockerfile文件创建镜像-对docker(3)的改进

    在<docker(3)docker下的centos7下安装jdk>中,当进入容器后,执行 java命令 不能运行,需要执行source /etc/profile才能执行.如果采用Docke ...

  6. Outlook 2010中263邮箱客户端设置

    Outlook 2010中263邮箱客户端设置 1.首次添加电子邮箱账户:打开outlook,在账户设置和服务中分别选择:“手动配置服务器设置或其他服务器类型”,“Internet电子邮件” 2.在i ...

  7. java——newInstance()方法和new关键字

    https://www.cnblogs.com/liuyanmin/p/5146557.html 这两个都可以创建一个对象,那么这样个东西有什么不一样呢?什么时候用new,什么时候用newInstan ...

  8. 获取url传的参数转变为对象的方法

    function GetRequest() { var url = location.search; //获取url中"?"符后的字串 var theRequest = new O ...

  9. redis要注意的一些知识

    除了存取数据,redis还可以支持mq等操作,这里面有些小细节,需要注意一下: ---------------------------------------- 1.事务处理 大家都说redis支持事 ...

  10. AndroidStudio3.0的安装和配置笔记

    开发Android项目要搭建开发环境.可以选择使用Eclipse安装ADT插件来开发安卓项目,也可以使用谷歌的官方IDE——AndroidStudio3.0. AndroidStudio3.0安装的具 ...