【LeetCode练习题】Maximum Depth of Binary Tree
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.
求二叉树的高度,即从根节点到叶节点上所有节点的数量。
解题思路:
这题是难度系数1,频率1的题目……用到的知识点是二叉树DFS(深度遍历)。
一个计数变量count,一个记录最大值变量max。
深度遍历的时候,遍历到的节点分三种情况:
- 叶节点。此时比较max和count的大小,将较大的那个赋值给max。
- 非叶节点,且有左节点无右节点,继续遍历其左节点,从左节点返回时要将count减1. 右节点不遍历了。
- 非叶节点,且有右节点无左节点,继续遍历其右节点,从右节点返回时要将count减1. 左节点不便利了。
max即为所求的最大深度。
代码如下:
class Solution {
public:
int maxDepth(TreeNode *root) {
int count = ,max = ;
depth(root,count,max);
return max;
}
void depth(TreeNode *root,int &count,int &max){
if(root){
if(!root->left && !root->right){
count++;
if(max < count)
max = count;
}
else{
count++;
if(root->left){
depth(root->left,count,max);
count--;
}
if(root->right){
depth(root->right,count,max);
count--;
}
}
}
}
};
难怪难度系数是1,可以简单成这个样子:
class Solution {
public:
int maxDepth(TreeNode *root) {
if(root == NULL) return ;
int l = maxDepth(root->left);
int r = maxDepth(root->right);
return l > r ? l + : r + ;
}
};
【LeetCode练习题】Maximum Depth of Binary Tree的更多相关文章
- Leetcode | Minimum/Maximum Depth of Binary Tree
Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth is the n ...
- leetcode 104 Maximum Depth of Binary Tree二叉树求深度
Maximum Depth of Binary Tree Total Accepted: 63668 Total Submissions: 141121 My Submissions Question ...
- LeetCode 104. Maximum Depth of Binary Tree C++ 解题报告
104. Maximum Depth of Binary Tree -- Easy 方法 使用递归 /** * Definition for a binary tree node. * struct ...
- [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 ...
- (二叉树 BFS DFS) 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 (二叉树的最大深度)
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
Problem: Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along ...
- leetcode 104 Maximum Depth of Binary Tree ----- java
Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...
- Java [Leetcode 104]Maximum Depth of Binary Tree
题目描述: Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along th ...
随机推荐
- java.lang.IllegalStateException at org.apache.catalina.connector.ResponseFacade
2012-10-4 19:50:37 org.apache.catalina.core.StandardWrapperValve invoke 严重: Servlet.service() for se ...
- xcode6和ios 8 百度无法定位解决
. @interface里: CLLocationManager *locationManager; . 初始化: locationManager = [[CLLocationManager allo ...
- 对c#剪切板Clipboard占用的问题一点解决方法
以前在百度写的文档,转移到此处 前几天做一个程序,其中有一个剪切板的操作,具体代码: Clipboard.SetText(“ABC”); 来完成一个复制字符串的操作. 自己调试通过,完全正常,然后就交 ...
- 机房收费系统合作版(三)——UI思索
案件追踪系统1.0暂告一段落.验收过程中.MR MI针对UI界面提出了很多自己的想法. 针对TGB项目的UI设计我也有我的感受: 1.不论大小项目.仅仅要一看界面准有70%到80%熟悉度. 2.一看这 ...
- LR如何监控tomcat性能
使用LoadRunner做性能测试,一般的直觉是LR只能完成脚本录制和编写模拟用户的请求行为,但是在某些情况下,要监控一些中间件或web服务器的性能时,就不能通过录制脚本来完成了,那么就需要手工来编写 ...
- 16. Linux 文件目录权限
# 文件参数 d:表示是一个目录-:表示这是一个普通的文件l: 表示这是一个符号链接文件,实际上它指向另一个文件b.c:分别表示区块设备和其他的外围设备,是特殊类型的文件s.p:这些文件关系到系统的数 ...
- 代码先行-log4Net初体验
1.安装 从http://logging.apache.org/log4net/download_log4net.cgi下载编译好的log4Net包并解压. 找到 bin\net\4.0\releas ...
- Global.asax使用1
Application_start: 1.第一个访问网站的用户会触发该方法.(针对访问的是asp.net应用程序的类,及ashx,aspx等才会触发) 2. 通常会在该方法里定义一些系统变量,如聊天室 ...
- 从C# String类理解Unicode(UTF8/UTF16)
上一篇博客:从字节理解Unicode(UTF8/UTF16).这次我将从C# code 中再一次阐述上篇博客的内容. C# 代码看UTF8 代码如下: string test = "UTF- ...
- hdu3599 War(最大流)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud War Time Limit: 2000/1000 MS (Java/Others ...