Leet Code OJ 112. Path Sum [Difficulty: Easy]
题目:
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]相关推荐
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- Leet Code OJ 125. Valid Palindrome [Difficulty: Easy]
题目: Given a string, determine if it is a palindrome, considering only alphanumeric characters and ig ...
- Leet Code OJ 20. Valid Parentheses [Difficulty: Easy]
题目: Given a string containing just the characters , determine if the input string is valid. The brac ...
- 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 ...
- 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 ...
最新文章
- tomcat session 共享
- pycharm 报黄(黄字、黄色)警告 Local variable 'xxx' might be referenced before assignment
- Android之常见面试题
- Jsoup从元素中抽取属性 文本和HTML
- php background-image,css background-image属性怎么用
- 花三分钟给女票写一个爬虫,做一回模范男友!
- 深圳大学二本计算机软件,深圳大学是几本(深圳大学是一本还是二本)
- “互联网+”大赛全市第三名软件杯全国第一名 - Cloud Lab商业策划书
- 学成在线首页——静态页面(html+css)素材链接放在文章结尾了
- 如何给Arduino项目添加音乐播放功能
- Python实时爬取斗鱼弹幕
- 自定义 View 之圆形、圆角、爱心、动态旗帜等图片效果
- 【Linux】一步一步学Linux——stty命令(243)
- 做短视频的几个小技巧,助你吸粉引流
- 【优化布局】基于粒子群算法求解带出入点的车间布局优化问题附matlab代码
- 室内定位技术的应用及室内定位技术的种类-新导智能
- linux图形界面和dos界面切换
- 制作自己的图片数据集(附代码)
- trie树之敏感词过滤算法
- GB 35114-2017 学习笔记