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.

Note: A leaf is a node with no children.

Example:

Given the below binary tree and sum = 22,

      5
/ \
4 8
/ / \
11 13 4
/ \ \
7 2 1

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

/**
* Definition for a binary tree node.
* 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 == NULL) return false;
if (root->val == sum && root->left == NULL && root->right == NULL)
{
return true;
} return hasPathSum(root->left, sum - root->val) || hasPathSum(root->right, sum - root->val); }
};

LeetCode 112. Path Sum(路径和是否可为sum)的更多相关文章

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

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

  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 (二叉树路径之和)

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

  5. LeetCode 112 Path Sum(路径和)(BT、DP)(*)

    翻译 给定一个二叉树root和一个和sum, 决定这个树是否存在一条从根到叶子的路径使得沿路全部节点的和等于给定的sum. 比如: 给定例如以下二叉树和sum=22. 5 / \ 4 8 / / \ ...

  6. 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 ...

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

  8. [leetcode] #112 Path Sum (easy)

    原题链接 题意: 给定一个值,求出从树顶到某个叶(没有子节点)有没有一条路径等于该值. 思路: DFS Runtime: 4 ms, faster than 100.00% of C++ class ...

  9. LeetCode 112. Path Sum 二叉树的路径和 C++

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

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

随机推荐

  1. MATLAB 下GUI的学习

    做界面,然后在对应程序里做调用. 0.打开文件:命令行输入 guide 或者 新建——图形用户界面1.文件-预设可以对字体.代码等进行设置.2.工具-GUI选项可以改变行为大小. 3,添加轴,面板,按 ...

  2. 文件夹备份tar脚本

    #! /bin/sh SOURCEDIR=/data/OA DESTDIR=/data/OAbackup TARDIR=/data/gz #拷贝源文件夹至目标文件夹 cp -r $SOURCEDIR ...

  3. SpringBoot——SpringBoot学习记录【一】

    前言 公司目前主要的业务,用的语言是java,所以学习下相关的技术呀,还好大学基础语言学的JAVA SpringBoot简介 官网 SpringBoot 简介 SpringBoot是用来简化Sprin ...

  4. KVC and Scalar

    https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/KeyValueCoding/DataTypes. ...

  5. 简要总结selenium四个工具组

    selenium 是基于WEB的自动化测试工具. 由以下几个工具组组成 1.selenium IDE: 一个火狐插件 点击这个插件就进入录制界面,能够记录用户的操作,并且将其导出为可重复使用的测试脚本 ...

  6. Linux定时任务crontab命令

    linux 系统则是由 cron (crond) 这个系统服务来控制的.Linux 系统上面原本就有非常多的计划性工作,因此这个系统服务是默认启动的.另外, 由于用户自己也可以设置计划任务,所以,Li ...

  7. 洛谷 P2577 [ZJOI2005]午餐 题解

    每日一题 day56 打卡 Analysis 算法:贪心+dp 容易想到贪心:吃饭慢的先打饭节约时间, 所以先将人按吃饭时间从大到小排序. 然后就是dp了: 首先,应该想到f[i][j][k]:前i个 ...

  8. Zookeeper的缺点

    工作中对Zookeeper都是间接比较浅的使用,性能也没太高的要求所以对它的缺点也没太深的认识,从网上评价看,它还是有不少问题的. 1. 原生客户端坑太多,Curator(Curator是Netfli ...

  9. luogu P1724 东风谷早苗

    二次联通门 : luogu P1724 东风谷早苗 /* luogu P1724 东风谷早苗 模拟即可 手抖 Y-- 打成 Y++ WA了两发.. */ #include <cstring> ...

  10. 第04组alpha冲刺(3/4)

    队名:斗地组 组长博客:地址 作业博客:Alpha冲刺(3/4) 各组员情况 林涛(组长) 过去两天完成了哪些任务: 1.收集各个组员的进度 2.写博客 展示GitHub当日代码/文档签入记录: 接下 ...