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 the values along the path equals the given sum.

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

判断二叉树是否存在一条从根节点到叶子节点的路径是的路径上节点的值之和等于给定的数sum,若存在返回true,否则返回false。

递归解法:每次递归sum都减去当前节点的值,当sum为0且当前节点为叶子节点时返回true;当sum不为0且当前节点为叶子节点时返回false;当sum不为0且当前

节点不为叶子节点时,根据当前节点的左右子树的情况,返回左子树的递归调用或右子树的递归调用或者是两者的或(||)。

代码如下:

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

leetcode 112的更多相关文章

  1. [LeetCode] #112 #113 #437 Path Sum Series

    首先要说明二叉树的问题就是用递归来做,基本没有其他方法,因为这数据结构基本只能用递归遍历,不要把事情想复杂了. #112 Path Sum 原题链接:https://leetcode.com/prob ...

  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) 10

    112. 路径总和 112. Path Sum 题目描述 给定一个二叉树和一个目标和,判断该树中是否存在根节点到叶子节点的路径,这条路径上所有节点值相加等于目标和. 说明: 叶子节点是指没有子节点的节 ...

  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 、 113. Path Sum II 、437. Path Sum III

    112. Path Sum 自己的一个错误写法: class Solution { public: bool hasPathSum(TreeNode* root, int sum) { if(root ...

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

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

  8. Java实现 LeetCode 112 路径总和

    112. 路径总和 给定一个二叉树和一个目标和,判断该树中是否存在根节点到叶子节点的路径,这条路径上所有节点值相加等于目标和. 说明: 叶子节点是指没有子节点的节点. 示例: 给定如下二叉树,以及目标 ...

  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. php解压 tar.gz 格式文件

    1.运用php自带压缩与归档扩展(phar) $phar = new PharData('song.tar.gz'); //路径 要解压的文件 是否覆盖 $phar->extractTo('c: ...

  2. VLC开发相关

    1. libvlc在release下时,会出错.解决办法 project->linker->optimization->references->  NOREF 2. Iibvl ...

  3. MySQL利用Navicat导出数据字典

    这里算是一个小技巧 利用mysql的information_schema中的COLUMNS表 和navicat中的导出功能实现快速导出数据字典 CREATE TEMPORARYTABLE `COLUM ...

  4. Zend Guard Run-time support missing问题的解决

    Zend Guard不仅可以实现对PHP应用的脚本进行加密保护和对PHP应用的产品进行商业许可证管理,还可以为许多软件生产商.IT服务提供商提供完善的加密和安全的产品发布系统. 虽然现在可以成功加密p ...

  5. 在dom4j中使用XPath

    package com.wzh.test.xpath; import java.io.File; import org.dom4j.Document; import org.dom4j.Documen ...

  6. Python中在脚本中引用其他文件函数的方法

    在导入文件的时候,Python只搜索当前脚本所在的目录,加载(entry-point)入口脚本运行目录和sys.path中包含的路径例如包的安装地址.所以如果要在当前脚本引用其他文件,除了将文件放在和 ...

  7. 使用t-sql从身份证号中提取生日

    使用t-sql从身份证号中提取生日,一下是转换16位身份证号的例子,仅供参考. create function getDateFromID( ) ) returns datetime as begin ...

  8. [ActionScript 3.0] Away3D 天空盒(skybox)例子

    /* SkyBox example in Away3d Demonstrates: How to use a CubeTexture to create a SkyBox object. How to ...

  9. 自动生成Makefile时,关于Makefile.am编写

    最近编译一个项目的程序时,二十几个源代码文件放在六个文件夹中,而且各个文件中头文件互相包含.以前写过编译这样组织的源码的makefile,所以这次也就直接写了. 确实因为各个文件间的头文件互相包含,造 ...

  10. Android 媒体键监听以及模拟媒体键盘的实现 demo

    有时我们需要程序模拟按钮或点击,而手机本身又没有,哪么可以采取其它方式 模拟实现,最后再去实际设备去测试(前期一般都拿不到设备): 如上一首,下一首,暂停等,手机上是没有的,但有些设备上是有的,所以我 ...