题目:
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,

return true, as there exist a root-to-leaf path 5->4->11->2 which sum is 22.

翻译:
给定一个二叉树和一个和sum,检测这个二叉树是否存在一个从根节点到叶子节点的路径,每个节点的和加起来是sum。

分析:
这道题目实现起来并不复杂,但有一些特殊取值的定义,题目说的不是很清楚,比如是空树的时候,返回什么,根据OJ的结果,空树是直接返回false的。还有一个可能出错的地方是在叶子节点的判断上,例如”[1,2] sum=1”这个输入,”1”这个节点有左子树”2”而没有右子树,而计算到”1” 这个节点时值刚刚是sum的1,笔者的第一版程序返回了true,但实际上这个节点并不是一个叶子节点。

代码:

/*** Definition for a binary tree node.* public class TreeNode {*     int val;*     TreeNode left;*     TreeNode right;*     TreeNode(int x) { val = x; }* }*/
public class Solution {public boolean hasPathSum(TreeNode root, int sum) {if(root==null){return false;}else{if(root.left==null&&root.right==null&&sum==root.val){return true;}if(hasPathSum(root.left, sum-root.val)){return true;}if(hasPathSum(root.right, sum-root.val)){return true;}}return false;}
}

Leet Code OJ 112. Path Sum [Difficulty: Easy]相关推荐

  1. Leet Code OJ 1. Two Sum [Difficulty: Easy]

    题目: Given an array of integers, return indices of the two numbers such that they add up to a specifi ...

  2. Leet Code OJ 223. Rectangle Area [Difficulty: Easy]

    题目: Find the total area covered by two rectilinear rectangles in a 2D plane. Each rectangle is defin ...

  3. Leet Code OJ 202. Happy Number [Difficulty: Easy]

    题目: Write an algorithm to determine if a number is "happy". A happy number is a number def ...

  4. Leet Code OJ 344. Reverse String [Difficulty: Easy]

    题目: Write a function that takes a string as input and returns the string reversed. Example: Given s ...

  5. Leet Code OJ 28. Implement strStr() [Difficulty: Easy]

    题目: Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if ne ...

  6. Leet Code OJ 125. Valid Palindrome [Difficulty: Easy]

    题目: Given a string, determine if it is a palindrome, considering only alphanumeric characters and ig ...

  7. Leet Code OJ 20. Valid Parentheses [Difficulty: Easy]

    题目: Given a string containing just the characters , determine if the input string is valid. The brac ...

  8. Leet Code OJ 189. Rotate Array [Difficulty: Easy]

    题目: Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the ar ...

  9. Leet Code OJ 66. Plus One [Difficulty: Easy]

    题目: Given a non-negative number represented as an array of digits, plus one to the number. The digit ...

最新文章

  1. tomcat session 共享
  2. pycharm 报黄(黄字、黄色)警告 Local variable 'xxx' might be referenced before assignment
  3. Android之常见面试题
  4. Jsoup从元素中抽取属性 文本和HTML
  5. php background-image,css background-image属性怎么用
  6. 花三分钟给女票写一个爬虫,做一回模范男友!
  7. 深圳大学二本计算机软件,深圳大学是几本(深圳大学是一本还是二本)
  8. “互联网+”大赛全市第三名软件杯全国第一名 - Cloud Lab商业策划书
  9. 学成在线首页——静态页面(html+css)素材链接放在文章结尾了
  10. 如何给Arduino项目添加音乐播放功能
  11. Python实时爬取斗鱼弹幕
  12. 自定义 View 之圆形、圆角、爱心、动态旗帜等图片效果
  13. 【Linux】一步一步学Linux——stty命令(243)
  14. 做短视频的几个小技巧,助你吸粉引流
  15. 【优化布局】基于粒子群算法求解带出入点的车间布局优化问题附matlab代码
  16. 室内定位技术的应用及室内定位技术的种类-新导智能
  17. linux图形界面和dos界面切换
  18. 制作自己的图片数据集(附代码)
  19. trie树之敏感词过滤算法
  20. GB 35114-2017 学习笔记

热门文章

  1. map()和zip()操作
  2. 最短路径之Floyd算法
  3. 彻底理解 Python 生成器
  4. MFC 界面美化 Skinmagic
  5. 【玩转cocos2d-x之三十】点九图和输入框的使用
  6. 网狐棋牌(八) 异步引擎 和 网狐棋牌(九) 服务引擎概览
  7. VC++调用UpdateLayeredWindow实现半透明窗体【转】
  8. 两个大文件找出相同的一条记录
  9. 计算机网络 | 传输层 :UDP与TCP协议详解
  10. 基于MySQL和DynamoDB的强一致性分布式事务实践