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

 /**
* 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. SAP 打开账期

    1.先OB52修改账期: 如下界面开得公司9000下面 7.8月份的账期 2.mmpv 关闭上两个账期 3.mmrv 查看现在账期情况

  2. PHP 数组函数整理

    如果你已经使用了一段时间PHP的话,那么,你应该已经对它的数组比较熟悉了——这种数据结构允许你在单个变量中存储多个值,并且可以把它们作为一个集合进行操作. 经常,开发人员发现在PHP中使用这种数据结构 ...

  3. linux 下两台电脑之间ssh无密码连接

    例子:在192.168.0.12使用tecmint用户,连接192.168.0.11主机上的sheena用户 Step 1: Create Authentication SSH-Kegen Keys ...

  4. 重写,重载,super,this,继承

    重写:overwrite/override 子类根据需要对从基类继承来的方法进行重写. 重写方法必须与被重写方法有相同的方法名,参数列表和返回类型. 重写方法不能使用比被重写方法更严格的访问权限. 重 ...

  5. 如何在 IIS 中设置 HTTPS 服务

    Windows Server2008.IIS7启用CA认证及证书制作完整过程 这篇文章介绍了如何安装证书申请工具: 如何在iis创建证书申请: 如何使用iis申请证书生成的txt文件,在工具中开始申请 ...

  6. 通过pycurl模块添加put和delete请求

    原文链接: http://anupamshakya.blogspot.com/2013/07/implementation-of-put-and-delete-in.html

  7. spider

    from lxml import etreeimport requestsdef getHtml(html): novelcontent = requests.get(html).content re ...

  8. ICE系列之3对象接口定义语言——slice

         Slice 定义由编译器编译到特定的实现语言 .编译器把与语言无关的定 义翻译成针对特定语言的类型定义和 API.开发者使用这些类型和 API 来 提供应用功能,并与 Ice 交互.用于各种 ...

  9. IOS 代码块传值

    #import <UIKit/UIKit.h> typedef void (^MyBlock)(NSString*); @interface SecondViewController : ...

  10. String 与 byte[]相互转换

    1. 从byte[]转换成string string result = System.Text.Encoding.UTF8.GetString(byteArray); 2.从string 转换成byt ...