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 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)
return sum==root.val; return hasPathSum(root.left,sum-root.val)||hasPathSum(root.right,sum-root.val); }
}
leetcode 112 Path Sum ----- 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 ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- mysql数据库 安装 (原创)
1.首先下载mysql 数据库,我安装的是mysql5.1版本,直接安装就行,但是如果以后你想在数据库里显示中文的话,要把字符集设置为gb2312 2.配置数据源odbc. mysql-connec ...
- 第48套题【tarjan】【图&树的连通性】【并查集】
Problem 1 图的连通性
- HDU 2255 二分图最佳匹配 模板题
题目大意: 给定每一个人能支付的房子价值,每个人最多且必须拥有一套房子,问最后分配房子可得到的最大收益 抄了个别人的KM模板,就这样了... #include <cstdio> #incl ...
- PL/SQL
function & procedure packages function --> arguments or parameters with arguments, IN, read o ...
- 使用Google API 下拉刷新或者增加数据 SwipeRefreshLayout
贴出布局代码: <android.support.v4.widget.SwipeRefreshLayout android:id="@+id/id_swipe_ly" and ...
- 黑马程序员——C语言基础语法 关键字 标识符 注释 数据及数据类型
Java培训.Android培训.iOS培训..Net培训.期待与您交流! (一下内容是对黑马苹果入学视频的个人知识点总结) (一)C语言简单介绍 (1)C语言程序是由函数组成的任何C语言程序都是由一 ...
- MapReduce数据流(一)
在上一篇文章中我们讲解了一个基本的MapReduce作业由那些基本组件组成,从高层来看,所有的组件在一起工作时如下图所示: 图4.4高层MapReduce工作流水线 MapReduce的输入一般来自H ...
- Oracle GoldenGate 12c实时捕获SQL Server数据
在Oracle GoldenGate 12c中,对一些最新的数据库提供了支持,比如SQL Server 2012/2014,当然12c也支持sql server 2008.主要新增特性有: 捕获进程可 ...
- Notification通知栏
Notification通知栏 首先实现的功能就是通知栏显示Notification,Notification是显示在系统的通知栏上面的,所以Notification 是属于进程之前的通讯.进程之间的 ...
- html<textarea>标签
最近在项目中页面回显<textarea>的值,可是设置了value属性怎么也回显不出来,后来才弄清楚,原来想要设置<textarea>的文本,不是使用value,而是如下方式: ...