[Java代码] [Leetcode] Maximum and Minimum Depth of Binary Tree 二叉树的最小最大深度
来源:互联网 发布:我国域名总数2016年6月 编辑:程序博客网 时间:2024/05/17 00:17
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.
递归法 复杂度时间 O(N) 空间 O(H) 递归栈空间
思路简单的递归。递归条件是,它的最大深度是它左子树的最大深度和右子树最大深度中较大的那个加1。基础条件是,当遇到空节点时,返回深度为0。该递归的实质是深度优先搜索。
代码public class Solution {
}
Minimum Depth of Binary TreeGiven a binary tree, find its minimum depth.
The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.
递归法 复杂度时间 O(N) 空间 O(H) 递归栈空间
思路当求最大深度时,我们只要在左右子树中取较大的就行了,然而最小深度时,如果左右子树中有一个为空会返回0,这时我们是不能算做有效深度的。所以分成了三种情况,左子树为空,右子树为空,左右子树都不为空。当然,如果左右子树都为空的话,就会返回1。
代码http://www.nvzi91.cn/niaodaoyan/29950.html广度优先搜索 复杂度时间 O(N) 空间 O(B)
思路递归解法本质是深度优先搜索,但因为我们是求最小深度,并不一定要遍历完全部节点。如果我们用广度优先搜索,是可以在遇到第一个叶子节点时就终止并返回当前深度的。
代码迭代加深有限深度优先搜索 复杂度时间 O(N) 空间 O(D)
思路英文名是Iterative Deepening Depth Limited Search.然而,广度优先搜索有一个致命缺陷就是,一旦分支变多,消耗空间就太大。这里我们还有改进的余地,就是用迭代加深的有限深度优先搜索。该算法每次迭代是一次有限深度优先搜索,如果本轮迭代没有发现目标(这题的目标是第一个叶子结点),则增加深度上限重新进行有限深度优先搜索。读者可能觉得这样会带来很多重复计算,但实际上经过数学证明后可以发现,该算法的时间复杂度和广度优先搜索是在一个数量级的,并没有太大的增加,而他的空间消耗仅仅是限制的深度。详情请翻阅Artificial Intelligence : A Modern Approach。
代码- [Java代码] [Leetcode] Maximum and Minimum Depth of Binary Tree 二叉树的最小最大深度
- Maximum and Minimum Depth of Binary Tree 二叉树的最小最大深度 [java]
- leetcode解题之Maximum/Minimum Depth of Binary Tree Java版(树的最大、最小深度)
- LeetCode Minimum Depth of Binary Tree 最小深度二叉树
- Leetcode Minimum Depth of Binary Tree 二叉树最小深度
- LeetCode:minimum-depth-of-binary-tree(二叉树最小深度)
- 【LeetCode】Maximum Depth of Binary Tree (二叉树最大深度)
- Leetcode Maximum Depth of Binary Tree 二叉树最大深度
- Minimum Depth of Binary Tree 二叉树的最小深度
- Minimum Depth of Binary Tree 二叉树的最小深度
- Minimum Depth of Binary Tree 二叉树的最小深度
- Maximum Depth of Binary Tree 二叉树的最大深度
- Maximum Depth of Binary Tree 二叉树的最大深度
- Maximum Depth of Binary Tree 二叉树的最大深度
- Maximum Depth of Binary Tree 二叉树的最大深度
- maximum-depth-of-binary-tree(二叉树的最大深度)
- Tree-----求树的最大深度和最小深度(104. Maximum Depth of Binary Tree 111. Minimum Depth of Binary Tree)
- leetcode Minimum&&Max Depth of Binary Tree 求树的最小&&最大深度
- mysql有用的函数集锦
- 获取字的高度和宽度
- struct 字节对齐详解与大小端模式
- GCC 编译详解
- 网易2016研发工程师编程题
- [Java代码] [Leetcode] Maximum and Minimum Depth of Binary Tree 二叉树的最小最大深度
- 菜鸟成长记-代理使用场景
- Java中使用Jedis操作Redis
- CentOS SELinux
- wireshark常用的过滤命令(协议分析利器,你值得拥有。)
- Python学习笔记--装饰器
- Andrew Ng机器学习笔记+Weka相关算法实现(三)神经网络和参数含义
- Python:鼠标滑动时坐标变化显示
- java工程师最新面试题(java语言基础)