LeetCode: Path Sum 解题报告
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.

SOLUTION 1:
相当简单的题目,用递归解决。base case: 如果root是叶子,并且sum为root的值,返回true.
否则在左右分别 找一下就好(调用递归),任何一边返回true都可以的。
https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/tree/hasPathSum.java
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);
}
LeetCode: Path Sum 解题报告的更多相关文章
- LeetCode: Combination Sum 解题报告
Combination Sum Combination Sum Total Accepted: 25850 Total Submissions: 96391 My Submissions Questi ...
- 【LeetCode】124. Binary Tree Maximum Path Sum 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcode ...
- 【LeetCode】112. 路径总和 Path Sum 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS 回溯 BFS 栈 日期 题目地址:https ...
- LeetCode: Minimum Path Sum 解题报告
Minimum Path Sum Given a m x n grid filled with non-negative numbers, find a path from top left to b ...
- LeetCode: Binary Tree Maximum Path Sum 解题报告
Binary Tree Maximum Path SumGiven a binary tree, find the maximum path sum. The path may start and e ...
- 【LeetCode】931. Minimum Falling Path Sum 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 相似题目 参考资料 日期 题目地址:htt ...
- 【LeetCode】64. Minimum Path Sum 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- LeetCode 2 Add Two Sum 解题报告
LeetCode 2 Add Two Sum 解题报告 LeetCode第二题 Add Two Sum 首先我们看题目要求: You are given two linked lists repres ...
- LeetCode 1 Two Sum 解题报告
LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...
随机推荐
- 【LeetCode】216. Combination Sum III
Combination Sum III Find all possible combinations of k numbers that add up to a number n, given tha ...
- scp的两种方式
如果host A 与 host B建立了信任连接(B有A的public key),那么从A向B传送文件,或者从B上传回文件都可以省略密码.但是前提是命令是在A上执行的. 从A向B拷贝文件 on hos ...
- NBUT [1475] Bachelor
[1475] Bachelor http://acm.nbut.cn:8081/Problem/view.xhtml?id=1475 时间限制: 1000 ms 内存限制: 65535 K 问题描述 ...
- SSM框架配置文件
1.Spring <?xml version="1.0" encoding="UTF-8"?> <beans:beans xmlns=&quo ...
- 安装Tomcat的Apr
转:http://www.cnblogs.com/littlehb/archive/2013/04/02/2994785.html 安装Tomcat的Apr,提升性能 发现 Tomcat 可以用 Ap ...
- android异步处理机制
昨天面试被提问android的异步处理机制有哪些,他说处理new thread还有哪种方式,我说implement runnable,他说不是,比如intentservice. 我说那还有asyncT ...
- 【转载】Chrome插件开发 尝试
本来来自 http://www.cnblogs.com/rufus-hua/ 1.新建文件夹 如图:整个项目的结构 2.新建一个名为manifest.json的文件,编码模式为utf-8,(可以先建好 ...
- .NET Core 2.0 Cookie中间件 权限验证
:在ConfigureServices添加Cookie中间件,使用自定义Scheme services.AddAuthentication(options=> { options.Default ...
- Photoshop做32位带Alpha通道的bmp图片
原文链接: http://blog.sina.com.cn/s/blog_65c0cae801016e5u.html 批量制作32位带Alpha通道的bmp图片,可以制作一个动作,内容可以如下: ...
- (面试题)两个对象值相同 (x.equals(y) == true) ,但却可有不同的 hash code ,这 句话对不对
答:不对,有相同的 hash code这是java语言的定义:1) 对象相等则hashCode一定相等:2) hashCode相等对象未必相等 1.如果是基本变量,没有hashcode和equals方 ...