原题链接

题意:

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

思路:

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. 社会不是承认有学历的人, 而是承认努力过得人, 而且是真正努力过不是穷忙的人(没有学历就要多付出一倍的努力)good

    送你一句 这就是你水平差的理由? 楼主你工资低是因为你技术不行, 不想努力然后怪罪学历, 为什么学历高的混得好, 因为学历高的人努力过, 你没学历技术还不行, 凭什么证明你努力过, 社会不是承认有学历 ...

  2. c#与JAVA利用SOCKET实现异步通信的SanNiuSignal.DLL已开源

    大家好,前段时间C#的SanNiuSignal.DLL已开源;因部分用户特需要JAVA版的SanNiuSignal;现在只能把半成品先拿出来暂时给他们用了,以后再慢慢改进; JAVA版目前已实现跟C# ...

  3. Ajax中post与get的区别

    get和post都是向服务器发送一种请求,只是发送机制不同 . 1. GET可以通过在请求URL上添加请求参数, 而POST请求则是作为HTTP消息的实体内容发送给WEB服务器. 2. get方式请求 ...

  4. shell多线程之进程间通信(3)

    之前的文章依赖是1对1或1多对的,但每个任务的前置任务都只有1个. 本文的核心在于一个任务依赖于多个任务的执行完成,如上图所示,这个任务就是fact,只有new和dviduser两个任务都完成的情况下 ...

  5. Java集合框架Collection(1)ArrayList的三种遍历方法

    ArrayList是java最重要的数据结构之一,日常工作中经常用到的就是ArrayList的遍历,经过总结,发现大致有三种,上代码: package com.company; import java ...

  6. 关于web系统整体优化提速总结

    关于web系统整体优化提速总结 一.背景 随着公司业务的拓展,随之而来就是各种系统横向和纵向的增加,PV.UV也都随之增加,原有的系统架构和模式慢慢遇上了瓶颈,需要逐步的对系统从整体上进行改造升级,通 ...

  7. Sqoop 的基本使用

    目录 一.Sqoop 基本命令 1. 查看所有命令 2. 查看某条命令的具体使用方法 二.Sqoop 与 MySQL 1. 查询MySQL所有数据库 2. 查询指定数据库中所有数据表 三.Sqoop ...

  8. spring 5.x 系列第16篇 —— 整合dubbo (代码配置方式)

    文章目录 一. 项目结构说明 二.项目依赖 三.公共模块(dubbo-ano-common) 四. 服务提供者(dubbo-ano-provider) 4.1 提供方配置 4.2 使用注解@Servi ...

  9. Docker配置容器位置和小技巧

    Docker使用小技巧 清理全部停止的docker容器 有时候我们会有很多已经停止的容器或者由于错误强制退出不能用的容器,那我们就需要删除了,但是我们一个一个的rm删除很麻烦,有多少容器就要rm多少次 ...

  10. SSM(五)Mybatis配置缓存

    1.在没有配置的情况下,mybatis默认开启一级缓存. Object object=mapper.getXxx(object); Object object2=mapper.getXxx(objec ...