题目描述

给定一个二叉树和一个值sum,判断是否有从根节点到叶子节点的节点值之和等于sum的路径,
例如:
给出如下的二叉树,sum=22,
             5
             / \
           4    8
           /      / \
         11  13  4
         /  \          \
        7    2        1
返回true,因为存在一条路径5->4->11->2的节点值之和为22



Given a binary tree and a sum, determine if the tree has a root-to-leaf
path such that adding up all the values along the path equals the given
sum.
For example:
Given the below binary tree andsum = 22,
             5
             / \
           4    8
           /      / \
         11  13  4
         /  \          \
        7    2        1

return true, as there exist a root-to-leaf path5->4->11->2which sum is 22.

示例1

输入

复制

{1,2},0

输出

复制

false
示例2

输入

复制

{1,2},3

输出

复制

true


/**
 * struct TreeNode {
 *    int val;
 *    struct TreeNode *left;
 *    struct TreeNode *right;
 * };
 */

class Solution {
public:
    /**
     *
     * @param root TreeNode类
     * @param sum int整型
     * @return bool布尔型
     */
    bool hasPathSum(TreeNode* root, int sum) {
        // write code here
        if (root==NULL){
            return false;
        }
        if(root->left ==NULL && root->right==NULL && sum-root->val==0)
            return true;
        return hasPathSum(root->left, sum-root->val)||hasPathSum(root->right, sum-root->val);
    }
};

leetcode 38:path-sum的更多相关文章

  1. [LeetCode] 437. Path Sum III_ Easy tag: DFS

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

  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] 113. Path Sum II 路径和 II

    Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...

  4. [LeetCode] 437. Path Sum III 路径和 III

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

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

  6. LeetCode 437. Path Sum III (路径之和之三)

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

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

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

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

  9. 动态规划小结 - 二维动态规划 - 时间复杂度 O(n*n)的棋盘型,题 [LeetCode] Minimum Path Sum,Unique Paths II,Edit Distance

    引言 二维动态规划中最常见的是棋盘型二维动态规划. 即 func(i, j) 往往只和 func(i-1, j-1), func(i-1, j) 以及 func(i, j-1) 有关 这种情况下,时间 ...

  10. [Leetcode Week14]Path Sum II

    Path Sum II 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/path-sum-ii/description/ Description Giv ...

随机推荐

  1. 55.Qt-将界面程序封装成静态库

    1.生成dll 然后选择创建静态链接库: 创建的时候,记得勾选QtGui,并且修改pro文件,添加下面1句(这样就可以创建界面了): 修改staticlib.cpp: #include "s ...

  2. SpringBoot整合Mybatis 编译失败:找不到符号 程序包不存在

    问题描述:jpa不好用,转用mybatis,配置多数据源  但是配置后无法编译mvn clean install  dao层这些报错,找不到entity的包     问题解决:罪魁祸首:热部署的部分 ...

  3. 霍夫曼编码(Huffman)

    题目:有一个字符串:cabcedeacacdeddaaaba,问题: (1)采用霍夫曼编码画出编码的过程,并写出各字符的编码 (2)根据求得的编码,求得各编码需要的总位数 (3)求出整个字符串总编码长 ...

  4. 【Linux教程】Linux系统零基础编程入门,想当大神?这些你都要学

    ✍ 文件和文件系统 文件是Linux系统中最重要的抽象,大多数情况下你可以把linux系统中的任何东西都理解为文件,很多的交互操作其实都是通过文件的读写来实现的. 文件描述符 在Linux内核中,文件 ...

  5. firstBlog

    第一篇博客 markdown语法的学习 基本语法 标题与段落 在文字前面加1-6个#号来表示来代表一级标题到六级标题 一级标题 五级标题 列表 对于无序列表,可以使用字符-,+和*作为列表的标记 - ...

  6. centos8平台使用pidstat监控cpu/内存/io

    一,安装pidstat: 1,安装 [root@localhost yum.repos.d]# yum install sysstat 2,查看版本: [root@localhost ~]# pids ...

  7. Harbor 安装教程

    Harbor 安装教程 一. CentOS设置 1. 更换阿里源 curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com ...

  8. QJsonObject 遍历

    遍历QjsonObject方式 方式一 QJsonObject::const_iterator it = l_obj.constBegin(); QJsonObject::const_iterator ...

  9. 两分钟搞定VS下第三方库的配置(以GNU Regex Library库为例)

    写C的朋友大概知道导入一个库的痛苦,特别是在宇宙第一IDE--VS下更是无从下手,生怕一不小心就把VS搞崩了,而VS的卸载过程又是一个十分头疼的过程.所以,这里特此开了一篇如何在VS下配置第三方库的博 ...

  10. C++学习---二叉树的输入及非递归遍历

    二叉树的二叉链表存储表示如下 //二叉树的二叉链表存储表示 typedef struct BiTNode { char data;//结点数据域 struct BiTNode* lchild, * r ...