【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 ...
随机推荐
- [转]依赖注入框架Autofac的简单使用
本文转自:http://www.nopchina.net/post/autofac.html 话说nopcommerce底层用到了autofac框架,这里转了一篇文章简单说明一下: Autofac是一 ...
- cucumber 文件目录结构和执行顺序
引用链接:http://www.cnblogs.com/timsheng/archive/2012/12/10/2812164.html Cucumber是Ruby世界的BDD框架,开发人员主要与两类 ...
- mongodb Gridfs操作
GridFS 介绍 GridFS是MongoDB规范用于存储和检索大文件,如图片,音频文件,视频文件等.这是一种文件系统用来存储文件,但数据存储于MongoDB集合中.GridFS存储文件比其文档大小 ...
- 【R语言进行数据挖掘】回归分析
1.线性回归 线性回归就是使用下面的预测函数预测未来观测量: 其中,x1,x2,...,xk都是预测变量(影响预测的因素),y是需要预测的目标变量(被预测变量). 线性回归模型的数据来源于澳大利亚的C ...
- dlopen与dlsym用法
dlopen和dlsym是用于打开动态链接库中的函数,将动态链接库中的函数或类导入到本程序中: dlopen函数: 功能:打开一个动态链接库 包含头文件: #include <dlfcn.h&g ...
- 洛谷 P1438 无聊的数列
题目背景 无聊的YYB总喜欢搞出一些正常人无法搞出的东西.有一天,无聊的YYB想出了一道无聊的题:无聊的数列...(K峰:这题不是傻X题吗) 题目描述 维护一个数列{a[i]},支持两种操作: 1.1 ...
- vue计算属性无法监听到数组内部变化
计算属性可以帮助我们简化代码,做到实时更新,不用再自己添加function去修改data. 首先看一下计算属性的基本写法(摘自官网) var vm = new Vue({ el: '#demo', d ...
- C#值类型和引用类型与Equals方法
1. C#的值类型和引用类型 C#的对象里面有两种类型,一个是引用类型,一个是值类型,值类型和引用类型的具体分类可以看下面的分类. 在C#中,不管是引用类型还是值类型,他们都隐式继承Object类 ...
- javase(1)_基础语法
一.java概述 1.Java语言特点:纯面向对象(一切皆对象),平台无关(JVM屏蔽底层运行平台的差异),不同的平台有不同的JVM,JVM将程序翻译成当前操作系统能执行的程序,一次编译到处运行),健 ...
- Java异常 Exception类及其子类
C语言时用if...else...来控制异常,Java语言所有的异常都可以用一个类来表示,不同类型的异常对应不同的子类异常,每个异常都对应一个异常类的对象. Java异常处理通过5个关键字try.ca ...