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.

解题思路:

递归,JAVA实现如下:

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

最新文章

  1. Markdown 图片助手-MarkdownPicPicker
  2. Ajax Step By Step1
  3. yii2 数据导出 excel导出以及导出数据时列超过26列时解决办法
  4. cookie实现自动登录
  5. osg(OpenSceneGraph)学习笔记1:智能指针osg::ref_ptr<>
  6. 打开网页自动弹出qq客户端
  7. MogileFS
  8. QML Object Attributes QML对象属性
  9. 基于FPGA的RGB565_YCbCr_Gray算法实现
  10. 使用SAS和JavaScript前端上传Azure Bolb大文件
  11. Core Animation 文档翻译 (第八篇)—提高动画的性能
  12. [ZJOI2007] 矩阵游戏
  13. Book : <Hands-on ML with Sklearn & TF> pdf/epub
  14. webpack4 系列教程(十五):开发模式与webpack-dev-server
  15. 91. Reverse Linked List 反转链表
  16. 什么是DDoS攻击?DDoS防御的11种方针详解
  17. 使用lets encrypt获取免费ssl证书
  18. viewpager fragment 滑动界面
  19. 51nod 1564 区间的价值 | 分治 尺取法
  20. Selenium - 设置元素等待

热门文章

  1. mysql count(*) 和count(1)区别
  2. MFC中 给基于CFormView的单文档添加背景图片
  3. 【GLSL教程】(四)shder的简单示例 【转】
  4. FenceSyne, flush, wait
  5. MFC 消息类型
  6. 性能测试脚本开发(C&C#&Java)
  7. <<Python基础教程>>学习笔记 | 第04章 | 字典
  8. Vue框架引入JS库的正确姿势
  9. idea设置自定义图片
  10. ThreadLocal,LinkedBlockingQueue,线程池 获取数据库连接2改进