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. 转载__Android开源项目(二)

    http://www.csdn.net/article/1970-01-01/2815145 GitHub上的开源项目不胜枚举,通过这些项目,也能让开发者在应用开发过程中事半功倍,作为开发者的你,在用 ...

  2. ibatis插入数据后返回自增长的主键

    insert into testTable ( activity_id,activity_title values ( #{activityId,jdbcType=INTEGER}, #{activi ...

  3. JAVA关系运算符

    常用的关系运算符: 编号 关系运算符 说明 1 > 大于 2 < 小于 3 == 等于 4 != 不等于 5 >= 大于或等于 6 <= 小于或等于 用关系运算符判断后返回的值 ...

  4. 在PHPstorm编辑器中配置git环境

    在phpstorm编辑器中配置git环境,使得编程人员从git仓库中提交代码,克隆代码,,,更佳便利快捷,有利于提高项目的质量和效率 工具/原料   phpstorm编辑器,git客户端 win7或w ...

  5. webim-界面细节调整

    1)左侧css调整 3)界面和滚动条美化 8)界面

  6. Servlet中的cookie和session

    保存数据的2中方式 Cookie Session Cookie 我们可以将一些信息保存到cookie中,cookie存放在对应的浏览器目录里面.每个站点可以保存20个cookie,最大长度不超过4k. ...

  7. (medium)LeetCode 227.Basic Calculator II

    Implement a basic calculator to evaluate a simple expression string. The expression string contains ...

  8. sqoop安装

    环境:Hadoop 2.3.0 sqoop 1.4.5 1.下载并解压sqoop-1.4.6.bin__hadoop-2.0.4-alpha.tar.gz (解压完,名字会很长,可以根据需要自己修改下 ...

  9. nginx 状态码整理

    状态代码    状态信息     含义 100 Continue 初始的请求已经接受,客户应当继续发送请求的其余部分.(HTTP 1.1新)101 Switching Protocols 服务器将遵从 ...

  10. JavaScript对象的创建之动态原型方式

    为了让定义的方式更加符合java的需求,就把定义方法的原型代码放置在Person这个构造函数中. function Person(name,age,friends){ //属性在构造函数中定义 thi ...