Leetcoede 112 Path Sum 二叉树
二叉树的从叶子到根的和是否存在
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public: bool hasPathSum(TreeNode *root, int sum) {
if (!root)
{
return false;
}
if (!root->left && !root->right)
{
return sum == root->val;
}
return hasPathSum(root->left,sum - root->val)||hasPathSum(root->right,sum - root->val);
}
Leetcoede 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 ...
- 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 ☆(二叉树是否有一条路径的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 二叉树的路径和 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 、 113. Path Sum II 、437. Path Sum III
112. Path Sum 自己的一个错误写法: class Solution { public: bool hasPathSum(TreeNode* root, int sum) { if(root ...
- [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 笔记 112 - Path Sum
题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...
- [leetcode]124. Binary Tree Maximum Path Sum二叉树最大路径和
Given a non-empty binary tree, find the maximum path sum. For this problem, a path is defined as any ...
- [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 ...
随机推荐
- 使用Aspose插件将程序中的表格,导出生成excel表格
http://www.cnblogs.com/lanyue52011/p/3372452.html这个是原文地址 /// <summary> /// 点击按钮,将内存表导出excel表格! ...
- 关于.NET(c#)引用Dll后,找不到命名空间的问题
最近做了一个Silverlight项目,一个类库引用DLL后,找不到命名空间.于是各种google,方法如下: 1.类库与DLL的目标框架不一致: 2.删除obj下文件和清空bin下文件: 3.重装系 ...
- C# 特殊处理使用方法
1.时间处理 Model.PiDaiTime.ToString("yyyyMMdd") == "00010101" ? DateTime.Now.ToStrin ...
- php使用位与运算符【&】或【|】实现权限管理
权限值是这样的2^0=1,相应2进数为”0001″(在这里^我表示成”次方”,即:2的0次方,下同)2^1=2,相应2进数为”0010″2^2=4,相应2进数为”0100″2^3=8,相应2进数为”1 ...
- java环境配置总结
最近接触java,在环境配置上费了不少劲.总结一下: 1.首先安装jdk和Eclipse,jdk我安装的是1.6,Eclipse可以从官网下载:http://download.eclipse.org/ ...
- Lvs之NAT、DR、TUN三种模式的应用配置案例
LVS 一.LVS简介 LVS是Linux Virtual Server的简写,意即Linux虚拟服务器,是一个虚拟服务器集群系统.本项目在1998年5月由章文嵩博士成立,是中国国内最早出现的 ...
- ThinkPHP提交表单判断上传图片经验总结
在用TP框架开发程序处理接收到的表单的时候,要判断<input type="file" name="img">是否有上传图片,来决定是否要将对应的图 ...
- Linux中exec命令相关
Linux中exec命令相关 exec和source都属于bash内部命令(builtins commands),在bash下输入man exec或man source可以查看所有的内部命令信息. b ...
- 『TCP/IP详解——卷一:协议』读书笔记——04
2013-08-18 16:31:17 第2章 链路层 2.1 引言 链路层主要有三个目的: 为IP模块发送和接受IP数据报 为ARP模块发送ARP请求和接受ARP应答 为RARP发送RARP请求和接 ...
- Xamarin开发Android笔记:背景操作
使用Xamarin开发Android UI的时可能会遇到一些场景背景的问题,虽然可以利用多层或直接使用图片背景来完成,但这样会增加不少的资源消耗,最终导致内存溢出的情况.最好的方法还是利用内部方法或代 ...