[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 ...
随机推荐
- pycharm窗口选项卡管理
1.主题 我们已经注意到Pycharm的主编辑框是基于窗口选项卡机制显示的,Pycharm选项卡多种多样,这里我们将详细介绍这种选项卡机制. 2.激活的选项卡 每当我们打开一个Python文件时op ...
- kubernetes实战篇之windows添加自签ca证书信任
系列目录 由于服务端设置了https访问,因此如果通过浏览器访问时会提示证书不被信任,但是仍然可以通过处理继续访问.但是在自动化环境中,都是通过命令来请求的,这样不受信任的https就会报错误,这样我 ...
- IIS7上传4M文件以上文件出现“Post大小超出允许的限制”错误解决方法
在web.config文件中的system.web节点中添加如下这句,即40M <system.web> <httpRuntime maxRequestLength = " ...
- WebFlux 集成 Thymeleaf 、 Mongodb 实践 - Spring Boot(六)
这是泥瓦匠的第105篇原创 文章工程: JDK 1.8 Maven 3.5.2 Spring Boot 2.1.3.RELEASE 工程名:springboot-webflux-5-thymeleaf ...
- openstack namespace 的应用
查看虚拟机网络连通性 1.neutron port-list | grep IP 2.neutron port-show ID 查看subnet 3.neutron subnet-show ID 查看 ...
- Python初学者的经历
刚开始安装了个python3.6的版本,自己写了个hello world ,发现可以运行,后面又网上找到了下载酷狗音乐的代码,结果报各种包没有,使用pip安装也各种安装不起来 又从网上找了python ...
- CSS中浮动的使用
CSS有两个性质 第一个是 :继承性 第二个是:层叠性: 选择器的一种选择能力,谁的权重大就选谁 { 里面分两种情况: 分别是 选中和没选中. 1.选不中的情况下,走继承性,(font,color,t ...
- jQuery调整表列(左右拉动调整列宽)插件__colResizable,动态列如何使用
官网地址:http://www.bacubacu.com/colresizable/ 这里值得注意的是,如果是动态加入的列,则需要先清理调用插件生成的class,id和div之后再重新调用才会有作用. ...
- Failed to start Docker Application Container Engine.
[root@dockertest ~]# systemctl status docker.service● docker.service - Docker Application Container ...
- python 基本数据类型之字符串功能
字符串常用功能: # name.upper() #全部大写变小写 # name.lower() #全部小写变大写 # name.split() #分割 # name.find() #找到指定子序列的索 ...