Java [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 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.
解题思路:
递归法。
代码描述:
/**
* 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;
if(root.left == null && root.right == null && root.val == sum)
return true;
return hasPathSum(root.left, sum - root.val) || hasPathSum(root.right, sum - root.val);
}
}
Java [Leetcode 112]Path Sum的更多相关文章
- [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 ...
- 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 ...
- [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 ...
- [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 ...
- Java for 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 ...
- leetcode 112 Path Sum ----- java
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...
- 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 ...
- 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 ...
- [Leetcode]112. Path Sum -David_Lin
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...
随机推荐
- linux文件系统创建文件的过程
创建一个文件最主要的步骤就是: 1.为文件创建一个文件目录项. 2.为文件创建一个inode结构并分配inode号,将inode编号与文件名映射关系保存在1中分配的文件目录项中. 3.将1中创建的文件 ...
- MacOS Cocos2d-x-3.2 创建HelloWorld项目
开发环境: Mac OSX 10.9.3 Cocos2d-x-3.2 首先,打开终端cd到目录/cocos2d-x-3.2/tools/cocos2d-console/bin下,运行cocos.py脚 ...
- Eclipse插件开发
最近在做Eclipse的插件开发,目前是在Eclipse3.x环境上进行开发,之后迁移到Eclipse4.x环境.会贡献在插件开发过程中遇到的所有问题以及相关技巧,敬请期待. SWT开发 JFace开 ...
- PHP之mysql_real_escape_string()函数讲解
定义和用法 mysql_real_escape_string() 函数转义 SQL 语句中使用的字符串中的特殊字符. 下列字符受影响: \x00 \n \r \ ' " \x1a 如果成功, ...
- HDU2594 Simpsons’ Hidden Talents 字符串哈希
最近在学习字符串的知识,在字符串上我跟大一的时候是没什么区别的,所以恶补了很多基础的算法,今天补了一下字符串哈希,看的是大一新生的课件学的,以前觉得字符串哈希无非就是跟普通的哈希没什么区别,倒也没觉得 ...
- SDUT2142数据结构实验之图论二:基于邻接表的广度优先搜索遍历
http://acm.sdut.edu.cn/sdutoj/showproblem.php?pid=2142&cid=1186 题目描述 给定一个无向连通图,顶点编号从0到n-1,用广度优先搜 ...
- 0环境设置 - AUTOTRACE设置
Autotrace是sqlplus的一个工具,用来显示所执行查询的查询计划 设置步骤 • cd [ORACLE_HOME]/rdbms/admin• log into SQL*Plus as SYST ...
- spring 与 CXF 整合 webservice 出现error “Unable to locate Spring NamespaceHandler for XML schema namespace” 总结
我试了多个版本的spring 发现 出现error : Unable to locate Spring NamespaceHandler for XML schema namespace 并非都是sp ...
- application.xml定时
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
- iOS开发--SWRevealViewController
SWRevealViewController 是一个比较好的实现Sliding功能的第三方类. 今天跟大家分享一下如何使用SWRevealViewController实现左右菜单滑动功能. 首先,在工 ...