leetcode 111 Minimum Depth of Binary Tree ----- java
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.
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public int minDepth(TreeNode root) { if( root == null){
return 0;
} Queue queue = new LinkedList<TreeNode>();
queue.add(root);
int dep = 1;
while( !queue.isEmpty() ){
int size = queue.size();
for( int i = 0;i<size;i++){
TreeNode node = (TreeNode) queue.poll();
if( node.left == null && node.right == null )
return dep;
if( node.left != null)
queue.add(node.left);
if( node.right != null)
queue.add(node.right); }
dep++;
}
return dep; }
}
leetcode 111 Minimum Depth of Binary Tree ----- java的更多相关文章
- [LeetCode] 111. Minimum Depth of Binary Tree 二叉树的最小深度
Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...
- [LeetCode] 111. Minimum Depth of Binary Tree ☆(二叉树的最小深度)
[Leetcode] Maximum and Minimum Depth of Binary Tree 二叉树的最小最大深度 (最小有3种解法) 描述 解析 递归深度优先搜索 当求最大深度时,我们只要 ...
- Leetcode 111 Minimum Depth of Binary Tree 二叉树
找出最短的从叶子到根的路径长 可以回忆Maximum Depth of Binary Tree的写法,只不过在!root,我把它改成了10000000,还有max函数改成了min函数,最后的值如果是1 ...
- Java for LeetCode 111 Minimum Depth of Binary Tree
Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...
- Java [Leetcode 111]Minimum Depth of Binary Tree
题目描述: Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along th ...
- Java实现LeetCode 111. Minimum Depth of Binary Tree
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * Tre ...
- LeetCode 111. Minimum Depth of Binary Tree (二叉树最小的深度)
Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...
- leetcode 111 minimum depth of binary tree
problem description: Given a binary tree, find its minimum depth. The minimum depth is the number of ...
- (二叉树 BFS DFS) leetcode 111. Minimum Depth of Binary Tree
Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...
随机推荐
- IT公司100题-5-查找最小的k个元素
问题描述: 输入n 个整数,输出其中最小的k 个. 例如输入8, 7, 6, 5, 4, 3, 2, 1这8 个数字,则最小的3 个数字为3, 2, 1. 分析: 时间复杂度O(nlogn)方法: ...
- Android 获取网络状态
1.检测网络是否可用 public boolean isNetWorkConnected() { ConnectivityManager cm = (ConnectivityManager)getSy ...
- 在oracle中创建空间索引
Oracle spatial可以方便的存储空间数据,大量的空间数据必需要使用空间索引去查询.在oracle中创建空间索引必需先建立元数据,否则无法创建索引.创建元数据的代码: insert into ...
- 推荐一款作图神器:ProcessOn
本人近日发现一款作图神器:ProcessOn 它是一款在线的作图工具,完全国产,前台是用HTML5 Canvas加javascript做绘图,后台用java实现数据处理和图片生成, 整站UI基本类似 ...
- S5PV210之beep-bus模型 linux3.0.8驱动
目录: 一. bus-driver-device模型 二. 运行结果,及错误解决 三. 怎样利用以有的driver device驱动来写自已的beep-driver-device 驱动 四 ...
- 测试V模型
一:测试V模型 RAD(Rap Application Development 快速引用开发)模型是软件开发过程中的一个重要模型,由于模型构图形似字母V,所以又称软件开发的V模型.他通过开发和测试同时 ...
- iframe和form表单的target应用简单例子
iframe和form表单的target属性 Problem: 刷新主页面中的其中一个iframe,其他内容不变 Solution: main.jsp <body onload=" ...
- 使用HttpOnly提升Cookie安全性
在介绍HttpOnly之前,我想跟大家聊聊Cookie及XSS. 随着B/S的普及,我们平时上网都是依赖于http协议完成,而Http是无状态的,即同一个会话的连续两个请求互相不了解,他们由最 ...
- sql server和oracle的差异
.部分SQL语句差异 (1)SQL:select top 10 * from table ORA: select * from table where rownum<11(2)SQL:S ...
- vim的Tab设置为4个空格
vim /etc/vimrc 1 set ts=42 set expandtab3 set autoindent 按tab键时产生的是4个空格,这种方式具有最好的兼容性.