Minimum Depth of Binary Tree ——LeetCode
Given 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.
https://leetcode.com/problems/minimum-depth-of-binary-tree/
题意就是给定一个二叉树,找出从根节点到叶子节点最低的高度,直接写了个递归,算出所有叶子节点的高度,取最小值。
另外一种很显然就用层次遍历(BFS), 就是按层次遍历这棵树,发现当前节点是叶子节点,就直接返回这个节点的高度。这里有个问题就是怎么记录当前的高度呢?我是这么做的:设置三个变量,upRow,downRow,height,分别代表队列中上一行节点数,下一行节点数,高度,当队列中上一行节点数为0,那么高度+1,把下一行的节点数量(downRow)赋给上一行(upRow),循环不变式是队列非空(!queue.isEmpty())。
Talk is cheap。
import java.util.LinkedList;
import java.util.List; /**
* Created with IntelliJ IDEA.
* User: Blank
* Date: 2015/3/21
*/
public class MinimumDepthofBinaryTree { public int minDepth(TreeNode root) {
int left = Integer.MAX_VALUE, right = Integer.MAX_VALUE;
if (root == null)
return 0;
if (root.right == null && root.left == null)
return 1;
if (root.left != null)
left = minDepth(root.left) + 1;
if (root.right != null)
right = minDepth(root.right) + 1;
return Math.min(left, right);
} public int minDepth2(TreeNode root) {
if (root == null) {
return 0;
}
int upRow = 1, downRow = 0, height = 0;
List<TreeNode> queue = new LinkedList<>();
queue.add(root);
while (!queue.isEmpty()) {
TreeNode node = queue.get(0);
queue.remove(0);
if (node.left == null && node.right == null)
return height + 1;
if (node.left != null) {
queue.add(node.left);
downRow++;
}
if (node.right != null) {
queue.add(node.right);
downRow++;
}
upRow--;
if (upRow == 0) {
height++;
upRow = downRow;
downRow = 0;
}
}
return height;
}
}
Minimum Depth of Binary Tree ——LeetCode的更多相关文章
- Minimum Depth of Binary Tree [LeetCode]
Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...
- Minimum Depth of Binary Tree leetcode java
题目: Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the ...
- LeetCode:Minimum Depth of Binary Tree,Maximum Depth of Binary Tree
LeetCode:Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- [LeetCode] 111. Minimum Depth of Binary Tree ☆(二叉树的最小深度)
[Leetcode] Maximum and Minimum Depth of Binary Tree 二叉树的最小最大深度 (最小有3种解法) 描述 解析 递归深度优先搜索 当求最大深度时,我们只要 ...
- LeetCode OJ Minimum Depth of Binary Tree 递归求解
题目URL:https://leetcode.com/problems/minimum-depth-of-binary-tree/ 111. Minimum Depth of Binary T ...
- [Leetcode][JAVA] Minimum Depth of Binary Tree && Balanced Binary Tree && Maximum Depth of Binary Tree
Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth is the n ...
- 【LeetCode练习题】Minimum Depth of Binary Tree
Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth is the n ...
- LeetCode My Solution: Minimum Depth of Binary Tree
Minimum Depth of Binary Tree Total Accepted: 24760 Total Submissions: 83665My Submissions Given a bi ...
随机推荐
- YYCache 源码分析(二)
本文分析YYMemoryCache实现原理: YYMemoryCache是内存缓存,所以存取速度非常快,主要用到两种数据结构的LRU淘汰算法 1.LRU Cache的容量是有限的,当Cache的空间都 ...
- JVM中java类的加载时机(转载:http://blog.csdn.net/chenleixing/article/details/47099725)
Java虚拟机把描述类的数据从Class文件加载到内存,并对数据进行校验.转换解析和初始化,最终形成可以被虚拟机直接使用的Java类型,这就是虚拟机的加载机制.类从被加载到虚拟机内存中开始,到卸载出内 ...
- Java基础知识强化之IO流笔记15:递归之删除带内容的目录案例
1. 需求:递归删除带内容的目录 分析: (1)封装目录 (2)获取该目录下的所有文件或者文件夹的File数组 (3)遍历该File数组,得到每一个File对象 (4)判断该File对 ...
- Strust2 <c:forEach> 循环控制标签
<c:forEach>为循环控制标签 语法:迭代一集合对象中的所有成员 <c:forEach [var="varName"] items="collec ...
- python----------反射和设计模式
反射: 把字符串映动态射成对象内存地址. hasattr():判断一个对象里是否有对应的字符串的方法 getattr():根据字符串去获取obj对象里的对应方法的内存地址. class Dog(obj ...
- Android中的Surface和SurfaceView
一.什么是Surface 简单的说Surface对应了一块屏幕缓冲区,每个window对应一个Surface,任何View都要画在Surface的Canvas上(后面有原因解释).传统的view共享一 ...
- HTTP简单理解
自己最近正在看 <Java Web 整合开发实战>这本书, 看到HTTP协议,感觉写的挺明白了,就把书中内容总结了下,记录在此. 超文本传输协议(HyperText Transfer Pr ...
- Python os常用模块
Python的标准库中的os模块包含普遍的操作系统功能.如果你希望你的程序能够与平台无关的话,这个模块是尤为重要的.即它允许一个程序在编写后不需要任何改动,也不会发生任何问题,就可以在Linux和Wi ...
- 判断iPhone和iPad 判断设备版本
//判断iPhone和iPad #define IS_IPHONE (!IS_IPAD) #define IS_IPAD (UI_USER_INTERFACE_IDIOM() != UIUserInt ...
- JS格式化数字金额用逗号隔开保留两位小数
JS格式化金额,正则方式修改. /** * 格式化金额 * @param {[type]} v [要转换的数字] * @param {[type]} len [小数点位数,默认2位] * @param ...