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.
题目标签:Tree
这道题目给了我们一个二叉树和一个sum, 让我们判断这个二叉树是否有至少一条path 的之和是等于sum的。利用preOrder 来遍历树,每次用sum 减去当前点的值,每当遇到一个leaf node 的时候检查sum 是不是等于0, 返回ture 和false。利用 || 来return 所有的boolean 值, 至少有过一个true,一个path之和等于sum, 总的boolean 就是true。
Java Solution:
Runtime beats 13.93%
完成日期:07/03/2017
关键词:Tree
关键点:当是leaf node 的时候检查sum;利用 || return两个children的返回值
/**
* 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; sum -= root.val; if(root.left == null && root.right == null)
{
if(sum == 0)
return true;
else
return false;
} return hasPathSum(root.left, sum) || hasPathSum(root.right, sum);
}
}
参考资料:
http://www.cnblogs.com/springfor/p/3879825.html
LeetCode 算法题目列表 - LeetCode Algorithms Questions List
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 ☆(二叉树是否有一条路径的sum等于给定的数)
Path Sum leetcode java 描述 Given a binary tree and a sum, determine if the tree has a root-to-leaf pa ...
- 112. Path Sum二叉树路径和
[抄题]: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding ...
- LeetCode 112. Path Sum 二叉树的路径和 C++
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(路径和)(BT、DP)(*)
翻译 给定一个二叉树root和一个和sum, 决定这个树是否存在一条从根到叶子的路径使得沿路全部节点的和等于给定的sum. 比如: 给定例如以下二叉树和sum=22. 5 / \ 4 8 / / \ ...
- leetcode:Path Sum (路径之和) 【面试算法题】
题目: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up ...
- LeetCode 112. Path Sum(路径和是否可为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] 437. Path Sum III 路径和 III
You are given a binary tree in which each node contains an integer value. Find the number of paths t ...
- [LeetCode] 113. Path Sum II 路径和 II
Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...
随机推荐
- Java:实现对象的比较 comparable接口和comparator接口
在实际应用中,我们往往有需要比较两个自定义对象大小的地方.而这些自定义对象的比较,就不像简单的整型数据那么简单,它们往往包含有许多的属性,我们一般都是根据这些属性对自定义对象进行比较的.所以Java中 ...
- Akka(26): Stream:异常处理-Exception handling
akka-stream是基于Actor模式的,所以也继承了Actor模式的“坚韧性(resilient)”特点,在任何异常情况下都有某种整体统一的异常处理策略和具体实施方式.在akka-stream的 ...
- Linux Ubuntu从零开始部署web环境及项目 -----快捷键设置(四)
上篇将了如何在linux部署web项目,这篇介绍如何设置常用快捷键 一.路径快捷键设置 临时快捷键设置: 执行XShel,输入: alias 'aa=cd /etc/sysconfig' ...
- Linux Ubuntu从零开始部署web环境及项目-----搭建ssh环境(一)
linux搭建ssh环境 1,用户登录 成功输入用户名和密码后 进入Ubuntu界面 2,配置网络 参考:http://blog.csdn.net/liu782726344/article/deta ...
- StringBuffer类的构造方法
public StringBuffer():无参构造方法 public StringBuffer(int capacity):指定容量的字符串缓冲区对象(默认是16个字符) public String ...
- PuTsangTo-单撸游戏开发01 Flag与计划
先立下flag,至少1年之内坚持并2年之内完成自己的一个梦想--游戏开发. 没有参加培训也不打算参加培训,就纯靠业余时间自学并用自己的思路完成一整套游戏体系.做出此决心时也已经做好准备烂尾了,但是有种 ...
- CentOS7下安装MariaDB
环境:Window10 上建立 VMWare 虚拟机,EasyInstaller 方式安装 CentOS 7 1. “失败”的经历 备份原 repo 文件,并更改 yum 源(方法详见修改yum源)为 ...
- python基础之条件循环语句
前两篇说的是数据类型和数据运算,本篇来讲讲条件语句和循环语句. 0x00. 条件语句 条件语句是通过一条或多条语句的执行结果(True或者False)来决定执行的代码块. 可以通过下图来简单了解条件语 ...
- ThinkPHP控制器输出防止乱码小技巧
在控制器中加一句:试试看 header('content-type:text/html;charset=utf-8');
- 【译】Yarn上常驻Spark-Streaming程序调优
作者从容错.性能等方面优化了长时间运行在yarn上的spark-Streaming作业 对于长时间运行的Spark Streaming作业,一旦提交到YARN群集便需要永久运行,直到有意停止.任何中断 ...