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

要求求出是否有一条路径和与给出的值相等,注意中间节点与叶子节点的判断:

 /**
* 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;
return checkSum(root, sum);
} bool checkSum(TreeNode* root, int sum)
{
if(root != NULL && sum == root->val && root->left == NULL && root->right == NULL){
return true;//上面这个判断确实是叶子节点,值也同时满足
}
else if(root == NULL)
return false;
else
return checkSum(root->left, sum - root->val) || checkSum(root->right, sum - root->val);
}
};

java版本的如下,递归版本的没上面那么麻烦:

 public class Solution {
public boolean hasPathSum(TreeNode root, int sum) {
if(root == null)
return false;
if(root.left == null && root.right == null){
return sum == root.val;
}
return hasPathSum(root.left, sum - root.val) ||
hasPathSum(root.right, sum - root.val);
}
}

LeetCode OJ: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. [leetcode] Path sum路径之和

    要求给定树,与路径和,判断是否存在从跟到叶子之和为给定值的路径.比如下图中,给定路径之和为22,存在路径<5,4,11,2>,因此返回true;否则返回false. 5 / \ 4 8 / ...

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

  4. 【LeetCode】Path Sum(路径总和)

    这道题是LeetCode里的第112道题.是我在学数据结构——二叉树的时候碰见的题.题目要求: 给定一个二叉树和一个目标和,判断该树中是否存在根节点到叶子节点的路径,这条路径上所有节点值相加等于目标和 ...

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

  6. [LeetCode] 666. Path Sum IV 二叉树的路径和 IV

    If the depth of a tree is smaller than 5, then this tree can be represented by a list of three-digit ...

  7. [LeetCode] 113. Path Sum II 路径和 II

    Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...

  8. [LeetCode] 437. Path Sum III 路径和 III

    You are given a binary tree in which each node contains an integer value. Find the number of paths t ...

  9. [LeetCode] 437. Path Sum III_ Easy tag: DFS

    You are given a binary tree in which each node contains an integer value. Find the number of paths t ...

  10. [LeetCode] 113. Path Sum II 二叉树路径之和之二

    Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...

随机推荐

  1. 动态生成ABAP程序-资料

    参考程序: Tcode ABAPdocu--> BC - ABAP Programming--> The ABAP Programming Language--> Special T ...

  2. (转)fiddler使用简介--其二

    原文地址:http://www.cnblogs.com/miantest/p/7290176.html 在上一篇中介绍了Fiddler的基本使用方法.通过上一篇的操作我们可以直接抓取浏览器的数据包.但 ...

  3. 前端基础之JavaScript_(4)_js的作用域

    js作用域 作用域就是变量与函数的可访问范围,即作用域控制着变量与函数的可见性和生命周期.在JavaScript中,变量的作用域有全局作用域和局部作用域两种. js的作用域 1. 全局作用域(Glob ...

  4. oracle导入数据库报错:IMP-00019: 由于 ORACLE 错误 12899 而拒绝行 IMP-00003: 遇到 ORACLE 错误 12899

    主要是字符集 成 导入的Oracle服务器的字符集 对应不上. 以下方案为是修改服务器的字符集.    这样会影响之前的Oracle其他数据库的数据显示(正式服务器慎用) 个人认为应该修改导出文件的字 ...

  5. UI控件之UITextField

    UITextField:文本框:用来输入一行文本,父类是UIControl UITextField *field1=[[UITextField alloc]initWithFrame:CGRectMa ...

  6. mysql插入一张表里的数据到另一张表

    公司的一个项目,做报表--要关联的表结构比较多,最后决定把要用的数据集合到一张新表中,需要用到以下的sql语法......分享下: web开发中,我们经常需要将一个表的数据插入到另外一个表,有时还需要 ...

  7. Linux doxygen的安装与使用

    1.安装doxygen 目前最新版本的的doxygen是doxygen1.8.13,安装包可以在官网上下载,网址是:http://www.stack.nl/~dimitri/doxygen/downl ...

  8. python爬虫之html解析Beautifulsoup和Xpath

    Beautiifulsoup Beautiful Soup 是一个HTML/XML的解析器,主要的功能也是如何解析和提取 HTML/XML 数据.BeautifulSoup 用来解析 HTML 比较简 ...

  9. input ajax自动补全

    页面 <div class="manage-Car-add-info-sn"> <p style="width:25%; height:70%;floa ...

  10. ZooKeeper服务-一致性、实现

    实现 ZooKeeper服务有两种不同的运行模式.一种是“独立模式”(standalone mode),即只有一个ZooKeeper服务器.这种模式比较简单,适合于测试环境,但是不能保证高可用性和可恢 ...