原题链接

题意:

给定一个值,求出从树顶到某个叶(没有子节点)有没有一条路径等于该值。

思路:

DFS

Runtime: 4 ms, faster than 100.00% of C++

class Solution
{
public:
bool hasPathSum(TreeNode *root, int sum)
{
if (root == NULL)
return false;
if (root->val == sum && root->left==NULL && root->right==NULL)
return true; return (root->left != NULL && hasPathSum(root->left, sum - root->val)) || (root->right != NULL && hasPathSum(root->right, sum - root->val));
}
};

[leetcode] #112 Path Sum (easy)的更多相关文章

  1. leetcode 112. Path Sum 、 113. Path Sum II 、437. Path Sum III

    112. Path Sum 自己的一个错误写法: class Solution { public: bool hasPathSum(TreeNode* root, int sum) { if(root ...

  2. [LeetCode] 112. Path Sum 路径和

    Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...

  3. [LeetCode] 112. 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] 112. Path Sum ☆(二叉树是否有一条路径的sum等于给定的数)

    Path Sum leetcode java 描述 Given a binary tree and a sum, determine if the tree has a root-to-leaf pa ...

  5. LeetCode 112. Path Sum (二叉树路径之和)

    Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...

  6. Leetcode 112. Path Sum

    Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...

  7. leetcode 112 Path Sum ----- java

    Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...

  8. Java [Leetcode 112]Path Sum

    题目描述: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding ...

  9. [Leetcode]112. Path Sum -David_Lin

    Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...

随机推荐

  1. VS2010下编译配置Boost_1.53

    一.准备工作 1.下载最新版本的boost库.所在地址:boost_1_53_0.zip.官方推荐7z压缩格式的,因为其压缩效率更好,相应包的大小也比较小. 2.解压缩到指定目录,如C:\boost_ ...

  2. 获取原生DOM,diy脚手架,vue-clide使用,element-ui的使用

    一.获取原生DOM的方式 给标签或者属性添加ref属性 //1.添加属性 <div ref='shy'><div> <Home ref='home'></Ho ...

  3. ZooKeeper学习第七期--ZooKeeper一致性原理(转)

    转载来源:https://www.cnblogs.com/sunddenly/p/4138580.html 一.ZooKeeper 的实现 1.1 ZooKeeper处理单点故障 我们知道可以通过Zo ...

  4. linux Apache设置https访问以及加载mod_ssl.so模块以及问题解决

    开始之前的话: 1.配置好服务器防火墙的443端口规则: 2.购买好证书文件,我是沃通证书,准备好证书,这里不演示证书的购买和安装. 3.根据服务器类型下载文件,apache一共有4个文件 这里提供沃 ...

  5. spring 5.x 系列第15篇 —— 整合dubbo (xml配置方式)

    文章目录 一. 项目结构说明 二.项目依赖 三.公共模块(dubbo-common) 四. 服务提供者(dubbo-provider) 4.1 productService是服务的提供者( 商品数据用 ...

  6. Enter passphrase

    提示“Enter passphrase for key /root/.ssh/id_rsa.pub”让输入私钥,可不论输与不输都不能直接登录 解决方法: 在本地执行: eval `ssh-agent` ...

  7. laravel扩展包-私有库

    创建一个新的laravel项目 composer create-project --prefer-dist laravel/laravel laravel-package "5.5.*&qu ...

  8. CI控制器

    当控制器要继承自定义的控制器的时候,有特定的定义: application/core/MY_Controller <?php class MY_Controller extends CI_Con ...

  9. 使用SQL行转列函数pivot遇到的问题

    背景:对投票的结果按照单位进行汇总统计,数据库中表记录的各个账号对各个选项的投票记录.马上想到一个解决方案,先根据单位和选项进行Group By,然后再行转列得出单位对各个选项的投票情况. with ...

  10. J-link使用SWD下载的连线方式

    手头有两块开发板,一个是F103ZET6,另一个是C8T6.后者开发板没有JTAG口,所以只能用SWD下载和调试程序. 有如下总结: 1.有些开发板对boot的电平有要求,网上说boot0要接高电平. ...