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. git发布代码到github

    git是什么? 1.git主要是用于对版本进行管理的一个系统. 2.时刻保持数据完整性:SHA-1计算 3.文件的三种状态 已提交,已修改,已暂存 对应文件流转的三个工作区域:本地仓库,Git的工作目 ...

  2. SAP内存、ABAP内存、共享内存的 区别

    区别: (1)SAP内存使用 SET/GET parameters 方法: SET  PARAMETER  ID  ‘MAT’ field P_MATNR. GET  PARAMETER  ID  ‘ ...

  3. Log4j详细配置解释

    原文地址:https://www.cnblogs.com/godtrue/p/6444158.html log4j(七)——log4j.xml简单配置样例说明 一:测试环境与log4j(一)——为什么 ...

  4. 在python中是如何管理内存的

    python有一个私有堆空间来保存所有的对象和数据结构.作为开发者,我们无法访问它,是解释器在管理它,但是有了核心api后,我们可以访问一些工具.python内存管理器控制内存分配 内置垃圾回收器会回 ...

  5. 前端基础之jquery_mouse相关操作与不同

    jquery中mouse相关操作与不同 mousedown() //当鼠标指针移动到元素上方,并按下鼠标左键时,会发生 mousedown 事件 mouseup() //当鼠标指针移动到元素上方,并松 ...

  6. Java基础教程:泛型基础

    Java基础教程:泛型基础 引入泛型 传统编写的限制: 在Java中一般的类和方法,只能使用具体的类型,要么是基本数据类型,要么是自定义类型.如果要编写可以应用于多种类型的代码,这种刻板的限制就会束缚 ...

  7. 利用onresize使得div可以随着屏幕大小而自适应的代码

    原文:http://www.jb51.net/article/21831.htm 当我们让div居中时候,一般有两种方法,一种是固定左右宽度,也就是使用像素绝对定位:另一种是用百分比来相对定位,在这种 ...

  8. active scaffold

    模板文件路径:/.rvm/gems/ruby-2.2.3/gems/active_scaffold-3.5.3/app/views/active_scaffold_overrides

  9. 关于java登录界面

    import java.awt.*; 包含用于创建用户界面和绘制图形图像的所有类.   import javax.swing.*; 提供一组“轻量级”(全部是 Java 语言)组件,尽量让这些组件在所 ...

  10. Linux用户和用户组管理 用户配置和管理的相关文件

    用户信息文件 /etc/passwd 这个文件中保存的就是系统中所有的用户及其对应的用户主要信息. 文件格式 :  第1字段 第2字段 第3字段 第4字段 第5字段 第6字段 第7字段 用户名称 密码 ...