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. XHTML的使用规范

    一.XHTML的简介 XHTML指的是可扩展超文本标记语言 XHTML与HTML4.01几乎是相同的 XHTML是更严格更纯净的HTML版本 XHTML是以XML应用的方式定义的HTML 二.为什么使 ...

  2. apache重写规则自动追加查询参数QSA

    看好多大网站上的搜索都是以.html?keyword=手机&page=abc这个样子表现, 如: search_goods.html?q=%BF%D5%C6%F8%BE%BB%BB%AF%C6 ...

  3. jquery extend中

    var $=123; <src="jquery.js"> //加载jquery.js的时候           里面有句 _$=window.$,保存123的 //no ...

  4. SOAP: java+xfire(web service) + php客户端

    作者: 吴俊杰 web service这项技术暂不说它有多落伍,但是项目中用到了,没法逃避!    xml和json各有各的好处,但是JSON无疑是当今数据交互的主流了.客户soap服务器端用的是 j ...

  5. oracle查看所有表的数据量

    源地址:http://blog.csdn.net/zhanggnol/article/details/6683697 select t.table_name,t.num_rows from user_ ...

  6. Java中的装箱拆箱

    一)  装箱与拆箱 Java中有概念是一切皆对象,因为所有的类都默认继承自Object.但是,对于数据类型是个例外,如short,int,long,float,double, byte,char,bo ...

  7. (medium)LeetCode 236.Lowest Common Ancestor of a Binary Tree

    Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According ...

  8. ubuntu14.04配置Hive1.2.1

    1.添加环境变量:vi ~/.bashrc #HIVE VARIABLES START export HIVE_HOME=/usr/local/hive-1.2.1 export PATH=$PATH ...

  9. OC基础(17)

    ARC基本概念 ARC快速入门 ARC下的内存管理 ARC和MRC兼容和转换 *:first-child { margin-top: 0 !important; } body > *:last- ...

  10. HDU 4407 Sum 容斥原理

    Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Problem Desc ...