这道题是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(二叉树的最大深度)的更多相关文章

  1. [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 ...

  2. [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 ...

  3. [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 ...

  4. [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 ...

  5. 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 ...

  6. [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 ...

  7. 104 Maximum Depth of Binary Tree 二叉树的最大深度

    给定一个二叉树,找出其最大深度.二叉树的深度为根节点到最远叶节点的最长路径上的节点数.案例:给出二叉树 [3,9,20,null,null,15,7],    3   / \  9  20    /  ...

  8. LeetCode——Maximum Depth of Binary Tree

    LeetCode--Maximum Depth of Binary Tree Question Given a binary tree, find its maximum depth. The max ...

  9. leetcode 104 Maximum Depth of Binary Tree二叉树求深度

    Maximum Depth of Binary Tree Total Accepted: 63668 Total Submissions: 141121 My Submissions Question ...

随机推荐

  1. java的8大排序详解

    本文转自 黑色幽默Lion的博客 http://www.cnblogs.com/pepcod/archive/2012/08/01/2618300.html 最近开始学习java,排序的部分之前学C的 ...

  2. php调用c# webservice方法

    第一次用,通过,还没深入了解. 首先在php.ini中启用extension=php_soap.dll,重启apache. $Client=new SoapClient("url?wsdl& ...

  3. uvm_object ——太极

    无极生太极——无名天地之始 太极生两仪——有名万物之母   文件: $UVM_HOME/src/base/uvm_object.svh 类: uvm_object The uvm_object cla ...

  4. Ubuntu下软件的搜索与安装

    本文为笔者原创,首发于简书(点击这里查看). 小白玩转linux的第一个拦路虎就是软件的安装了.本文结合自己在Ubuntu14.04下软件安装经验做一个总结. 1.如何搜索软件? apt-cache ...

  5. An internal error occurred during: "Map/Reduce location status updater". java.lang.NullPointerException

    eclipse配置hadoop 2.6 服务器做的虚拟机,因为window是的hadoop会出现意想不到的错误,因为,我做了ubuntu的虚拟机供我使用 在虚拟机中进行映射设置 在eclipse中dr ...

  6. DRM 简介

    首先,我们对和DRM 相关的一些概念进行介绍. Buffer: 对于RAC 数据库,当一个数据块被读入到buffer cache后,我们就称其为buffer , cache fusion 会将这个bu ...

  7. 栈的应用——Rails

    一.题目描述 某城市有一个火车站,有n节车厢从A方向驶入车站,按进站顺序编号为1~n,经中转站C驶向B.中转站C,这是一个可以停放任意多节车厢的车站,但由于末端封顶,驶入C的车厢必须以相反的顺序驶出C ...

  8. map最基本操作

    #include<iostream> #include<map> using namespace std; int main() { /*map<int,char> ...

  9. 传统BP对比CNN

    传统BP vs CNN 存在2个问题 传统BP网络存在的问题: 权值太多,计算量太大 权值太多,需要大量样本进行训练 传统的BP来处理图像问题的话因为计算权值太多太大. 网络的建立要根据数据的大小来建 ...

  10. Catalan 数

    概要 在一些面试的智力题中会遇到此数的变形,如果完全不了解,直接想结果是很困难的,故在此简单介绍一下.   基本定义 Catalan 数的定义根据不同的应用环境有很多不同的定义方式,下面给出一个.   ...