/**
* 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. Python字符串拼接、格式化输出、深浅复制

    1.Python字符串拼接:方法挺多.挺好用的.灵活使用可使代码简洁.可读性好. #1.用4种方法,将列表li = ['I','python','like'], #里面的单词拼成: I**like** ...

  2. vim与vi操作

    vim是vi发展而来的文本编辑器: vi是最原始的文本编辑器: vi/vim的使用: 有三种模式:命令模式.输入模式.底线命令模式 命令模式: 输入 i 会进入输入模式 输入: 会进入底线命令模式 输 ...

  3. Rabbitmq相关学习网址

    1.安装文档: http://www.cnblogs.com/shuzhenyu/p/9823324.html 2.RabbitMq的整理 exchange.route.queue关系 https:/ ...

  4. 查询多列得到map与查询得到po对象

    import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; i ...

  5. C# 事务提交(非数据库)

    .Net 2.0开始支持 static void Main(string[] args) { using (TransactionScope ts = new TransactionScope()) ...

  6. eclipse安装阿里规范模板

    https://github.com/alibaba/p3c/tree/master/p3c-formatter 1.代码模板(含注释等) 2.代码格式化

  7. 转——深度学习之BN算法(Batch Normailization)

    Batch Normalization 学习笔记 原文地址:http://blog.csdn.net/hjimce/article/details/50866313 作者:hjimce 一.背景意义 ...

  8. my14_mysql指定时间恢复之模拟从库

    场景 *********************************线上库数据误删除,存在几天前的一份全备数据,现需要恢复这些误删除的数据本例方案:在另外一台服务器上,恢复全备,搭建binlog ...

  9. C# 控制反转(IOC: Inverse Of Control) & 依赖注入(DI: Independence Inject)

    举例:在每天的日常生活中,我们离不开水,电,气.在城市化之前,我们每家每户需要自己去搞定这些东西:自己挖水井取水,自己点煤油灯照明,自己上山砍柴做饭.而城市化之后,人们从这些琐事中解放了出来,城市中出 ...

  10. 判断元素类型JS

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...