/**
* 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. Redis学习笔记(1)—— NoSQL&Redis简介

    一.NoSQL概述 1.1 什么是NoSQL NoSQL(NoSQL = Not Only SQL),意指“不仅仅是SQL”,是一项全新的数据库理念,泛指非关系型的数据库. 1.2 为什么需要NoSQ ...

  2. 关于c语言的位运算&,|,^(看懂汉字的都能看懂)

    其中|,&可以当作逻辑运算符,当|,&当成逻辑运算符时,与||,&&的用法基本相似,&&,||运算时会当前面的表达式能够决定整个表达式,则不进行对后面的 ...

  3. Go语言基础练习题系列2

    1.练习1 生成一个随机数,让一个用户去猜这个数是多少? 代码示例如下: package main import ( "fmt" "math/rand" //m ...

  4. Why Nexiq 125032 USB Link Truck diagnostic tool is so helpful ?

    As for as I am concerned , Heavy Duty Diagnostic Nexiq 125032 USB is a helpful tool , which has exce ...

  5. php数组·的方法3-数组指针

    /* * 数组指针函数 * */ //key() current() 指针一直停在第一位 不会下移 echo '<hr>'; $arr5 = array('name' => 'hxq ...

  6. SpringBoot初始教程之Servlet、Filter、Listener配置详解

    1.介绍 通过之前的文章来看,SpringBoot涵盖了很多配置,但是往往一些配置是采用原生的Servlet进行的,但是在SpringBoot中不需要配置web.xml的 因为有可能打包之后是一个ja ...

  7. oracle批量删除表

    select 'DROP TABLE '||TABLE_NAME||';' from user_tables where table_name like ‘%T_%’

  8. 统计学howto

    统计之都 http://cos.name/ 让你拥有超能力:程序员应该掌握的统计学公式 Statistical Formulas For Programmers http://www.evanmill ...

  9. 本地git的使用

    git和svn的解析 git 教程 git rebase的用法 attion: one:  git中是严格区分大小写的,文件名字大小写敏感 two:  git中分为:工作区,暂存区,分支 three: ...

  10. SourceTree 关于 .gitignore使用/下载

    # =============== # # Unity generated # # =============== # Temp/ Obj/ UnityGenerated/ Library/ Asse ...