[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 ...
随机推荐
- IT 外包中的甲方乙方,德国人,美国人,印度人和日本人印象杂谈
开篇介绍 最近经常和朋友聚会,三十而立的年龄自然讨论最多的就是各自的小家庭,如何赚钱,工作,未来的就业发展,职业转型等话题.还有各种跳槽,机会选择,甲方乙方以及外包中的各种趣事,外企与国内私企的发展机 ...
- itrator控制迭代次数
<s:iterator value="diys" status="d" begin="0" end="10" st ...
- STM32F103之DMA
一.背景: 需要使用STM32的DAC,例程代码中用了DMA,对DMA之前没有实际操作过,也很早就想知道DMA到底是什么,因此,看了一下午手册,代码和网上的资料,便有了此篇文章,做个记录. 二.正文: ...
- LOL 控制技能的解释
压制:硬控的一种,是指A在释放压制型技能的时候B无法进行任何动作,可以被 水银腰带 和 坩埚盖 解除(原来可以完全解掉,现在貌似只是能动,伤害还在继续,比如蚱蜢的R中,用水银解掉之后可以立即跑开,但是 ...
- PHP变量入门教程(2)超全局变量,总共9个
PHP 超全局变量 $GLOBALS 包含一个引用指向每个当前脚本的全局范围内有效的变量.该数组的键标为全局变量的 名称.从 PHP 3 开始存在 $GLOBALS 数组. $_SERVER 变量由 ...
- CentOS6.3安装MongoDB2.2 及 安装PHP的MongoDB客户端
下载源码:(放到 /usr/local/src 目录下) 到官网 http://www.mongodb.org/downloads 下载源码 https://fastdl.mongodb.org/li ...
- render()方法是render_to_response
自django1.3开始:render()方法是render_to_response的一个崭新的快捷方式, 前者会自动使用 RequestContext.而后者必须coding 出来,这是最明显的区别 ...
- [Android Pro] ScrollView使用fillViewport设置高度为MatchParent
reference to : http://blog.csdn.net/u012975705/article/details/49275001 之前遇到一问题,就是当使用ScrollView时,Scr ...
- python之OS模块详解
python之OS模块详解 ^_^,步入第二个模块世界----->OS 常见函数列表 os.sep:取代操作系统特定的路径分隔符 os.name:指示你正在使用的工作平台.比如对于Windows ...
- (转)Tomcat7+Redis存储Session
原创http://blog.csdn.net/caiwenfeng_for_23/article/details/45666831 PS:截止到2015-05-12前是不支持Tomcat8的,详情见官 ...