[leetcode] #112 Path Sum (easy)
题意:
给定一个值,求出从树顶到某个叶(没有子节点)有没有一条路径等于该值。
思路:
DFS
Runtime: 4 ms, faster than 100.00% of C++
class Solution
{
public:
bool hasPathSum(TreeNode *root, int sum)
{
if (root == NULL)
return false;
if (root->val == sum && root->left==NULL && root->right==NULL)
return true; return (root->left != NULL && hasPathSum(root->left, sum - root->val)) || (root->right != NULL && hasPathSum(root->right, sum - root->val));
}
};
[leetcode] #112 Path Sum (easy)的更多相关文章
- 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 路径和
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 ☆(二叉树是否有一条路径的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 ...
- 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 ...
- 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 -David_Lin
Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...
随机推荐
- vim好用的功能 sublime text2类似的实现系列一
sublime的跳转 功能 快捷键 备注 往回跳 alt+- 和vim一样方便,可以在页面些跳转,定位到上次编辑的地方 往前跳 alt+shift+- 无 定义或取消标记 ctrl+<F2> ...
- Access Violation分成两大类:运行期和设计期(很全的解释)
用Delphi开发程序时,我们可以把遇到的Access Violation分成两大类:运行期和设计期. 一.设计期的Access Violation 1.硬件原因 在启动或关闭Delphi IDE以 ...
- 客服端JavaScript线程模型
JavaScript语言核心并不包含任何线程机制,并且客服端JavaScript传统上没有定义任何线程机制.HTML5定义了一种作为后台线程的“WebWorker",但是客服端JavaScr ...
- 一个基于jQuery写的弹窗效果(附源码)
最近项目中频繁遇到需要弹出窗口的功能,一直使用浏览器默认的Alert和Confirm弹窗,感觉视觉效果不是那么好,而从网上下载的话又找不到合适的,找到的话有些也是十分臃肿,有时候感觉学习配置的功夫自己 ...
- [2017.02.15] 《C++Primer5》 复习笔记
编程语言主要是提供一个框架,用计算机能够处理的方式来表达问题的解决方法. 自定义数据类型class的基本思想是数据抽象dataabstraction和封装encapsulation.数据抽象是一种依赖 ...
- spring通过注解方式依赖注入原理 (私有成员属性如何注入)
一.spring如何创建依赖的对象 用过spring的都知道我们在dao.service层加上@repository.@Service就能将这两个对象交给spring管理,在下次使用的时候使用@res ...
- oracle10g登录em后,提示“java.lang.Exception: Exception in sending Request :: null”
出现错误时登录企业管理器时出现的界面 出现这种错误一般是因为没有设置时区,一般默认的是agentTZRegion=GMT,也就是GMT.所以大家只要设置了这个东西,然后重新启动dbconsole就可以 ...
- java.lang.ClassNotFoundException: org.apache.commons.fileupload.FileItemFactory 解决办法
解决办法:引入file upload 模块. 在POM文件中添加如下内容: <!-- file upload part --> <dependency> <groupId ...
- sqlserver/mysql按天,按小时,按分钟统计连续时间段数据
文 | 子龙 有技术,有干货,有故事的斜杠青年 一,写在前面的话 最近公司需要按天,按小时查看数据,可以直观的看到时间段的数据峰值.接到需求,就开始疯狂百度搜索,但是搜索到的资料有很多都不清楚,需要自 ...
- 点菜网---Java开源生鲜电商平台-系统架构图(源码可下载)
点菜网---Java开源生鲜电商平台-系统架构图(源码可下载) 1.点菜网-生鲜电商平台的价值与定位. 生鲜电商平台是一家致力于打造全国餐饮行业智能化.便利化.平台化与透明化服务的创新型移动互联网平台 ...