LeetCode - 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.
思路:
仍是递归,但是得注意(1) 必须是到 leaf node (2)Null结点的判断
package tree;
public class PathSum {
boolean isFirstRoot = true;
public boolean hasPathSum(TreeNode root, int sum) {
if (isFirstRoot && root == null) return false;
isFirstRoot = false;
if (root == null && sum == 0) return true;
if (root == null && sum != 0) return false;
if (root.left == null || root.right == null) return root.left == null ? hasPathSum(root.right, sum - root.val) : hasPathSum(root.left, sum - root.val);
return hasPathSum(root.left, sum - root.val) || hasPathSum(root.right, sum - root.val);
}
}
LeetCode - Path Sum的更多相关文章
- LeetCode:Path Sum I II
LeetCode:Path Sum Given a binary tree and a sum, determine if the tree has a root-to-leaf path such ...
- [LeetCode] Path Sum III 二叉树的路径和之三
You are given a binary tree in which each node contains an integer value. Find the number of paths t ...
- [LeetCode] Path Sum II 二叉树路径之和之二
Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...
- [LeetCode] 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] Path Sum IV 二叉树的路径和之四
If the depth of a tree is smaller than 5, then this tree can be represented by a list of three-digit ...
- LeetCode Path Sum IV
原题链接在这里:https://leetcode.com/problems/path-sum-iv/description/ 题目: If the depth of a tree is smaller ...
- [leetcode]Path Sum II
Path Sum II Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals ...
- LeetCode: Path Sum II 解题报告
Path Sum II Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals ...
- LeetCode: Path Sum 解题报告
Path Sum Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that addi ...
- [Leetcode] Path Sum II路径和
Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...
随机推荐
- 学习设计模式第三 - 基础使用UML表示关系
面向对象的思想中存在如下几种关系,一般为了方便交流都使用UML的类图来展现类之间的关系.所以了解类图中符号的含义对看懂类图,尤其是用类图展示的设计模式很有帮助.下面依次介绍这几种关系 类继承关系 继承 ...
- Python黑客编程2 入门demo--zip暴力破解
Python黑客编程2 入门demo--zip暴力破解 上一篇文章,我们在Kali Linux中搭建了基本的Python开发环境,本篇文章为了拉近Python和大家的距离,我们写一个暴力破解zip包密 ...
- JavaScript使用DeviceOne开发实战(四)仿优酷视频应用
开发之前需要考虑系统的差异性,比如ios手机没有回退键,所以在开发时一定要考虑二级界面需要有回退键,否则ios的手机就会陷入到这个页面出不去了.安卓系统有回退键,针对这个情况需要要求用户在3秒钟之内连 ...
- DISCUZ 自定义模板
DISCUZ 自定义模板 模板安装和维护 安装新模板 将模板template打包放在对应目录:template/ 后台 -> 界面 -> 风格管理 , 安装模板 后台 -> 界面 - ...
- [异常解决] 初玩SAE遇到的小问题——注册&创建项目+MyEclipse装插件直接部署+一个简单的JSP部署实现
① 新浪SAE快速上手教程:http://jingyan.baidu.com/season/43090 上面一个链接是针对PHP的相关介绍,如果用java还有点不一样,具体请看新浪SAE官网:http ...
- Open Source Cassandra Gitbook for Developer
Just released an open source Cassandra Gitbook for developer. http://teddymaef.github.io/learncassan ...
- linux下进程间通信
信号 信号是进程间相互传递消息的一种方法,只是用来通知某进程发生了什么事件,并不给进程传递任何数据. #include <sys/types.h> #include <unistd. ...
- swift 项目 oc 和 swift 混用,文件相互引用
创建swift工程后,如果后面想新建 oc 文件,这时会生成一个 AppName-Bridging-Header.h文件 一,在swift 文件中 1> 引用swift 文件 什么都不需要操作 ...
- GAMIT 10.50在Ubuntu 12.04系统下的安装
转载于:http://www.itxuexiwang.com/a/liunxjishu/2016/0225/162.html?1456480908 摘要:GAMIT/GLOBK是一套安装于Unix/L ...
- Redis主从复制问题和扩容问题的解决思路
转载于:http://www.itxuexiwang.com/a/shujukujishu/redis/2016/0216/106.html?1455867541 一.解决主从复制问题 当使用Redi ...