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.

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

112. Path Sum (判断路径和是否等于某值)的更多相关文章

  1. 112. Path Sum二叉树路径和

    [抄题]: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding ...

  2. LeetCode 112 Path Sum(路径和)(BT、DP)(*)

    翻译 给定一个二叉树root和一个和sum, 决定这个树是否存在一条从根到叶子的路径使得沿路全部节点的和等于给定的sum. 比如: 给定例如以下二叉树和sum=22. 5 / \ 4 8 / / \ ...

  3. LeetCode 112. Path Sum(路径和是否可为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] 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] 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 ...

  6. leetcode 437 Path Sum III 路径和

      相关问题:112 path sum /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNo ...

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

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

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

随机推荐

  1. 解决 Visual Studio For Mac 还原包失败问题

    体验了一把改名部最新的杰作,总体感觉挺好,也能看出微软在跨平台这方面所做出的努力. 可能是预览版的缘故,还是遇到一个比较大的问题,创建netcore项目后,依赖包还原失败,错误信息如下: 可以先试着手 ...

  2. Windows 动态库创建和使用 part 2

    一.Windows动态库的创建: 1.先选择 "DLL" 和 “控项目” 2.添加一个头文件,一个源文件  CppDll.h,CppDll.cpp,一个模块定义文件 CppDll. ...

  3. find命令结合cp bash mv 命令使用的4种方式

    工作经常需要用find结合其它命令一起使用,下面介绍4种结合方式. 例: 用find查找/data目录下,以.txt文件结尾的文件并复制到/tmp下 方法一 find与|xargs是黄金搭档,-t 参 ...

  4. 【理财】阅读:Millionaire Teacher

    书是本好书,就是翻译的中文书名让人无力吐槽了,叫<拿工薪,三十几岁你也能赚到600万>,浓浓的畅销书架味道有木有. 这本书作者极力推崇指数基金,以及全球极简投资配置策略.重点梳理了一下: ...

  5. java语言中public、private、protected三个关键字的用法,重写和重载的区别。

    java语言中public.private.protected三个关键字的用法,重写和重载的区别. 解答: 作用域 当前类 同包 子类 其它 public √ √ √ √ protected √ √ ...

  6. RGB565 转换 BMP24

    今晚有心情,就做完了BMP16(RGB565) 转换成BMP24 的小程序.其中最关键的地方是: // k,WORD类型,是RGB565 的一个点的数据(可以认为是灰度). r=(k & 0x ...

  7. js计算百分比

    function GetPercent(num, total) { /// <summary> /// 求百分比 /// </summary> /// <param na ...

  8. datagrid使用要点

    table自适应: (fit:true(设置table)) 列自动撑开:fitColumns: true,注意给列的width属性赋值

  9. 【转】Android横竖屏重力自适应

    通常我们的应用只会设计成横屏或者竖屏,锁定横屏或竖屏的方法是在manifest.xml文件中设定属性android:screenOrientation为"landscape"或&q ...

  10. apache 一个站点配置多个域名

      <VirtualHost *:80> ServerAdmin i@kuigg.com DocumentRoot /www/kuigg.com ServerName kuigg.com ...