[Leetcode] 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.
方法一:
层次遍历,整棵树的层数,即二叉树的最大深度。主体的代码还是在层次遍历的基础上改成的。
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int maxDepth(TreeNode *root)
{
if(root==NULL) return ;
int level=;
queue<TreeNode *> Q;
Q.push(root);
while( !Q.empty())
{
int levNum=;
int count=Q.size(); //不能和下面的while条件写成levNUm<Q.size()
//因为这样写以后,不遍历完整棵树,不会跳出
while(levNum<count)
{
TreeNode *temp=Q.front();
Q.pop();
if(temp->left)
Q.push(temp->left);
if(temp->right)
Q.push(temp->right);
levNum++;
}
level++;
}
return level;
}
};
方法二:
写法不一样,思想一样。
class Solution
{
public:
int maxDepth(TreeNode* root)
{
if(!root) return ;
queue<TreeNode*> Q; int nCount=; //某一层节点的个数
int nDepth=; //层数 //基于BFS,引入两个计数器
Q.push(root);
while(!Q.empty())
{
TreeNode* pCur=Q.front();
Q.pop();
nCount--; if(pCur->left)
Q.push(pCur->left);
if(pCur->right)
Q.push(pCur->right); if(!nCount) //保证遍历某一层时,深度不增加
{
nDepth++;
nCount=Q.size();
}
}
return nDepth;
}
}
方法三:
递归算法,终止条件为:节点不存在时,返回0,递归式为1+max(maxDepth(root->left),maxDepth(too->right)),即左右子树中的最大深度加上root所在层。
class Solution {
public:
int maxDepth(TreeNode* root) {
if (root==NULL) return ;
return + max(maxDepth(root->left), maxDepth(root->right));
}
};
[Leetcode] 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 ...
- [LintCode] 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二叉树的最大深度 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 ...
- 【LeetCode】Maximum Depth of Binary Tree(二叉树的最大深度)
这道题是LeetCode里的第104道题. 给出题目: 给定一个二叉树,找出其最大深度. 二叉树的深度为根节点到最远叶子节点的最长路径上的节点数. 说明: 叶子节点是指没有子节点的节点. 示例: 给定 ...
- 104 Maximum Depth of Binary Tree 二叉树的最大深度
给定一个二叉树,找出其最大深度.二叉树的深度为根节点到最远叶节点的最长路径上的节点数.案例:给出二叉树 [3,9,20,null,null,15,7], 3 / \ 9 20 / ...
- LeetCode——Maximum Depth of Binary Tree
LeetCode--Maximum Depth of Binary Tree Question Given a binary tree, find its maximum depth. The max ...
- leetcode 104 Maximum Depth of Binary Tree二叉树求深度
Maximum Depth of Binary Tree Total Accepted: 63668 Total Submissions: 141121 My Submissions Question ...
随机推荐
- Python里//与/的区别?
1.Python里面//的作用是除法取整,也就是直接取整数部分 例如:5//6=0; 56//3=18 2.而/的作用是直接进行常规的除法运算 例如:56/8=7 程序运算实例如下:
- VMWare Workstation新装CentOS 7无法联网解决办法
按照这位博主的方法成功解决:http://blog.csdn.net/deniro_li/article/details/54632949
- PAT-甲级解题目录
PAT甲级题目:点这里 pat解题列表 题号 标题 题目类型 10001 1001 A+B Format (20 分) 字符串处理 1003 1003 Emergency (25 分) 最短路径 ...
- Java 单例模式探讨
以下是我再次研究单例(Java 单例模式缺点)时在网上收集的资料,相信你们看完就对单例完全掌握了 Java单例模式应该是看起来以及用起来简单的一种设计模式,但是就实现方式以及原理来说,也并不浅显哦. ...
- “hello world!”团队第三次会议
团队“hello world!”团队召开的第三次会议.博客内容: 一.会议时间 二.会议地点 三.会议成员 四.会议内容 五.todo list 六.会议照片 七.燃尽图 一.会议时间 2017年10 ...
- Android开发 使用 adb logcat 显示 Android 日志
作者 : 万境绝尘 转载请著名出处 eclipse 自带的 LogCat 工具太垃圾了, 开始用 adb logcat 在终端查看日志; 1. 解析 adb logcat 的帮助信息 在命令行中输入 ...
- bootstrap使用中遇到的坑
一.例如: <div class="form-group"> <label class="control-label col-lg-3"> ...
- python学习笔记01:安装python
下载python: 从从https://www.python.org/downloads/下载python,根据操作系统的不同,选择不同的版本下载.注意:linux系统大多预装了python,可以直接 ...
- 移动端调试和fiddler移动端抓包使用
这里介绍一款移动端的调试工具以及抓包工具fiddler的使用.也是初次接触,算是初次接触的总结. 1,移动端调试工具.手机截图如下 代码实现 <!DOCTYPE html> <htm ...
- 解决ueditor编辑器图片在线管理图片无法显示
使用ueditor,点击在线管理,服务器图片路径显示不正确,如下图所示 查看源码,如下: 发现图片src中中间多了一长串的项目跟路径,解决的办法是 把 jsp/controller.jsp 里面 ...