[LintCode] Maximum Depth of Binary Tree 二叉树的最大深度
Given a binary tree, find its maximum depth.
The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.
Given a binary tree as follow:
1
/ \
2 3
/ \
4 5
The maximum depth is 3
.
LeetCode上的原题,请参见我之前的博客Maximum Depth of Binary Tree。
解法一:
class Solution {
public:
/**
* @param root: The root of binary tree.
* @return: An integer
*/
int maxDepth(TreeNode *root) {
if (!root) return ;
return + max(maxDepth(root->left), maxDepth(root->right));
}
};
解法二:
class Solution {
public:
/**
* @param root: The root of binary tree.
* @return: An integer
*/
int maxDepth(TreeNode *root) {
if (!root) return ;
int res = ;
queue<TreeNode*> q;
q.push(root);
while(!q.empty()) {
++res;
int n = q.size();
for (int i = ; i < n; ++i) {
TreeNode *t = q.front(); q.pop();
if (t->left) q.push(t->left);
if (t->right) q.push(t->right);
}
}
return res;
}
};
[LintCode] Maximum Depth of Binary Tree 二叉树的最大深度的更多相关文章
- [LeetCode] Maximum Depth of Binary Tree 二叉树的最大深度
Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...
- [LeetCode] 104. Maximum Depth of Binary Tree 二叉树的最大深度
Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...
- [Leetcode] Maximum depth of binary tree二叉树的最大深度
Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...
- LeetCode 104. Maximum Depth of Binary Tree二叉树的最大深度 C++/Java
Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...
- [LeetCode] 104. Maximum Depth of Binary Tree ☆(二叉树的最大深度)
描述 Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the l ...
- 104 Maximum Depth of Binary Tree 二叉树的最大深度
给定一个二叉树,找出其最大深度.二叉树的深度为根节点到最远叶节点的最长路径上的节点数.案例:给出二叉树 [3,9,20,null,null,15,7], 3 / \ 9 20 / ...
- 【LeetCode】Maximum Depth of Binary Tree(二叉树的最大深度)
这道题是LeetCode里的第104道题. 给出题目: 给定一个二叉树,找出其最大深度. 二叉树的深度为根节点到最远叶子节点的最长路径上的节点数. 说明: 叶子节点是指没有子节点的节点. 示例: 给定 ...
- leetcode 104 Maximum Depth of Binary Tree二叉树求深度
Maximum Depth of Binary Tree Total Accepted: 63668 Total Submissions: 141121 My Submissions Question ...
- Maximum Depth of Binary Tree 二叉树的深度
Given a binary tree,find its maximum depth. The maximum depth is the number of nodes along the longe ...
随机推荐
- 大熊君JavaScript插件化开发------(第一季)
一,开篇分析 Hi,大家!大熊君又来了,今天这系列文章主要是说说如何开发基于“JavaScript”的插件式开发,我想很多人对”插件“这个词并不陌生, 有的人可能叫“组件”或“部件”,这不重要,关键是 ...
- 大熊君大话NodeJS之------基于Connect中间件的小应用(Bigbear记事本应用之第一篇)
一,开篇分析 大家好哦,大熊君又来了,昨天因为有点个人的事没有写博客,今天又出来了一篇,这篇主要是写一个记事本的小应用,前面的文章, 我也介绍过“Connect”中间件的使用以及“Mongodb”的用 ...
- Login Reference for PhotoSomething
Android Background Processing with Handlers and AsyncTask and Loaders - Tutorial http://www.vogella. ...
- ThinkPHP之OAuth2.0环境搭建
几个比较好的超链接 1.http://www.tuicool.com/articles/u6beUju 2.http://leyteris.iteye.com/blog/1483403
- ajax请求webservice的过程中遇到的问题总结
前台用ajax的post方法,无法请求到webservice中的方法的时候,需要在配置文件中添加 web.config文件中的 <system.web> 节点下加入:<webServ ...
- 浏览器兼容innerText nextElementSibling firstElementChild
//下面是封装的方法,可以直接使用 //获dom对象的innerText的取值 function getInnerText(element){ //判断浏览器是否支持innerText if(type ...
- sdcms标签
模板防盗:<%if not in_sdcms then response.write("template load fail"):response.end() end if% ...
- word201612012
I/O (input/output) port / 输入/输出端口 IAS, Internet Authentication Service / Internet 验证服务 ICMP, Interne ...
- 【ASP.NET】利用Nuget打包package——GUI方式
GUI方式 通过GUI的方式,可以下载如下的软件 NuGetPackageExplorer 打包dll 1.打开软件,在Package Content处点击右键 ,选择Add Lib 2.在lib ...
- jstl
今日内容 l JSTL标签库 l EL函数 l 自定义标签库开发 l JSP模式和案例(*****) 1.1 上次课内容总结 JSP技术: * JSP:Java Server Pages * JSP运 ...