【LeetCode】Maximum Depth of Binary Tree(二叉树的最大深度)
这道题是LeetCode里的第104道题。
给出题目:
给定一个二叉树,找出其最大深度。
二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。
说明: 叶子节点是指没有子节点的节点。
示例:
给定二叉树[3,9,20,null,null,15,7],3
/ \
9 20
/ \
15 7返回它的最大深度 3 。
DFS 递归算法,简单的二叉树题。如果看不懂代码,建议学习一下二叉树,这可是基础啊!
给出代码:
递归法:
/**
* Definition for a binary tree node.
* 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 0;
int leftDepth=0;
if(root->left!=NULL)
leftDepth=maxDepth(root->left);
int rightDepth=0;
if(root->right!=NULL)
rightDepth=maxDepth(root->right);
return max(leftDepth,rightDepth)+1;
}
};
//精简版:
class Solution {
public:
int maxDepth(TreeNode* root) {
if(!root)return 0;
return 1+max(maxDepth(root->left),maxDepth(root->right));
}
};
迭代法:
/**
* Definition for a binary tree node.
* 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)return 0;
queue<TreeNode*> qt;
int max=0;
int preNodeCount=1;
int nodeCount=0;
qt.push(root);
while(qt.size()!=0){
TreeNode* tn=qt.front();
preNodeCount--;
qt.pop();
if(tn->left!=NULL){
qt.push(tn->left);nodeCount++;
}
if(tn->right!=NULL){
qt.push(tn->right);nodeCount++;
}
if(preNodeCount==0){
preNodeCount=nodeCount;
nodeCount=0;
max++;
}
}
return max;
}
};
给出结果:
两个结果都一样,之前做的时候还是 4ms 呢!可能是实例变多了。

给出总结:
能用递归法的就可以试一下用迭代法解决,果不其然,有所收获!一开始使用迭代法使用的是栈,使用栈无论怎么操作,都会产生无限循环的问题,无法得到最终解,因为一般来讲,堆栈适合用来求深度,这道题也是要求深度,可是万万没想到,这道题使用队列来实现迭代,我认为队列适合来求广度,但这题超出我的预料。到时候在找找看有没有用堆栈求解的代码。
哦,想起来了,或许可以用前中后序的代码做文章,改造一下用来求最大深度!!!
【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 ...
- [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 ...
- 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 ...
随机推荐
- Aop第一节
什么是AOP AOP(Aspect-OrientedProgramming,面向方面编程),可以说是OOP(Object-Oriented Programing,面向对象编程)的补充和完善.OOP引入 ...
- Java编程简介
作者:CHAITANYA SINGH 来源:https://www.koofun.com//pro/kfpostsdetail?kfpostsid=3 JAVA由Sun Microsystems In ...
- 2019/05/11 JAVA虚拟机原理
所谓虚拟机,就是一台虚拟的机器.他是一款软件,用来执行一系列虚拟计算指令,大体上虚拟机可以分为 系统虚拟机和程序虚拟机, 大名鼎鼎的Visual Box.Vmare就属于系统虚拟机,他们完全是对物理计 ...
- 【CSS】纯css实现立体摆放图片效果
1. 元素的 width/height/padding/margin 的百分比基准 设置 一个元素 width/height/padding/margin 的百分比的时候,大家可知道基准是什么? 举 ...
- selenium-Python之鼠标事件
通过click()来模拟鼠标的单击操作,鼠标还具有鼠标右击,双击,悬停甚至鼠标拖动等功能.在webdriver中,将这些鼠标操作方法封装在ActionChains类提供. ActionChains类提 ...
- HDU 1950 Bridging signals (LIS,O(nlogn))
题意: 给一个数字序列,要求找到LIS,输出其长度. 思路: 扫一遍+二分,复杂度O(nlogn),空间复杂度O(n). 具体方法:增加一个数组,用d[i]表示长度为 i 的递增子序列的最后一个元素, ...
- POJ 1155 TELE (树形DP,树形背包)
题意:给定一棵树,n个节点,其中有m个叶子表示的是用户,其他点表示中转器, 每条边都有权值,每个用户i愿意给的钱w[i],问如果在不亏钱的情况下能为多少用户转播足球比赛? 思路: 其实就是要选出部分叶 ...
- SpringMVC、Spring和Struts的区别
http://www.cnblogs.com/hhx626/p/6010293.html 导读:近期做到的项目中,用到的框架师SSM(SpringMVC+Spring+Mybatis),那么在这之前用 ...
- JS 、JQ 获取宽高总结 & JS中getBoundingClientRect的作用及兼容方案
1.getBoundingClientRect的作用 getBoundingClientRect用于获取某个html元素相对于视窗的位置集合. 执行 object.getBoundingClien ...
- Open Cascade:AIS_InteractiveContext如何调用函数选择AIS对象
AIS_InteractiveContext如何调用函数选择AIS对象 myAISContext->MoveTo(point.x, point.y, myView); myAISContext- ...