二叉树的从叶子到根的和是否存在

 /**
* 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)
{
return sum == root->val;
}
return hasPathSum(root->left,sum - root->val)||hasPathSum(root->right,sum - root->val);
}

Leetcoede 112 Path 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. 112. Path Sum二叉树路径和

    [抄题]: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding ...

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

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

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

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

  7. Leetcode 笔记 112 - Path Sum

    题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...

  8. [leetcode]124. Binary Tree Maximum Path Sum二叉树最大路径和

    Given a non-empty binary tree, find the maximum path sum. For this problem, a path is defined as any ...

  9. [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. 如何在asp.net中使用多线程及队列,异步处理一个耗时的任务(原创)

    最近想在使用.net 的队列处理一些耗时的工作.经过考虑,需要先设计一个类,类中包含一个静态的队列.主要是写队列和读取队列. public class PaperCalculator { // 用于存 ...

  2. mysql术语解释

    数据库(database): 数据表的集合: 数据表 (table):数据的矩阵: 列(column): 相同属性的数据的集合: 行(row): 一个对象的各种属性数据的集合: 冗余():一个字段在多 ...

  3. server 2008 IIS 搭建PHP运行环境

    本文以windows server 2008 r2 Enterprise作为操作系统,以IIS为web部署服务组件,配置PHP的服务器端执行环境,其中IIS版本为7.5,PHP版本为5.3. 注意:本 ...

  4. Daily Scrum 12.12

    今日完成任务: 解决文档上传时TagAssociation的建立异常问题:解决问题页面标签点击卡死的BUG. 发现问题: 文档下载量浏览量显示不正确: 文档打开时全都是同一个PDF: 右侧最佳资源的显 ...

  5. c#开发Mongo笔记第六篇

    之前写的五篇比较得到了大家的积极反馈,也有个别高手对我写我写出的代码进行了指教. 其中提到的我写的查询方法性能有问题,我想了想,如果mongo不是延时加载的话,那我的查询就真的有问题了,就成了查询出来 ...

  6. 2016/10/28 很久没更了 leetcode解题 3sum

    15. 3Sum Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Fi ...

  7. ActionScript语言函数重载

    更新:你见过JavaScript支持重载吗,规范就是这么定义的.如果不是研究Java和Flex对象的Serialization,我也不会注意它. 距离写这篇文章已有8年了,时光匆匆啊,今天整理资料时看 ...

  8. 20145225唐振远 《Java程序设计》第1周学习总结——小试牛刀

    20145225唐振远<Java程序设计>第1周学习总结——小试牛刀 教材学习内容总结 1.java语言概述:一门高级编程语言. 2.java语言的三种技术构架:java SE.java ...

  9. 纯css用图片代替checkbox和radio,无js实现方法

    html <ul id="is_offical_post_links"> <li> <label> <input type="c ...

  10. 【堆】【kd-tree】bzoj2626 JZPFAR

    用堆记录答案.看看当前点是否比堆顶更优. #include<cstdio> #include<queue> #include<cstring> #include&l ...