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的更多相关文章

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

  2. [LeetCode] 111. Minimum Depth of Binary Tree ☆(二叉树的最小深度)

    [Leetcode] Maximum and Minimum Depth of Binary Tree 二叉树的最小最大深度 (最小有3种解法) 描述 解析 递归深度优先搜索 当求最大深度时,我们只要 ...

  3. Leetcode 111 Minimum Depth of Binary Tree 二叉树

    找出最短的从叶子到根的路径长 可以回忆Maximum Depth of Binary Tree的写法,只不过在!root,我把它改成了10000000,还有max函数改成了min函数,最后的值如果是1 ...

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

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

  6. Java实现LeetCode 111. Minimum Depth of Binary Tree

    /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * Tre ...

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

  8. leetcode 111 minimum depth of binary tree

    problem description: Given a binary tree, find its minimum depth. The minimum depth is the number of ...

  9. (二叉树 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 ...

随机推荐

  1. sql思维

    写sql的思路不同于常规编程语言(C.python)等等.前者,考虑如何一步步地得到最终答案:后者,考虑如何一步步地收缩数据范围. 简而言之,前者是面向过程化(for each row do x),后 ...

  2. android之DOM生成与解析

    DOM解析不适合于进行大数据文件的操作,DOM解析适合于对文件进行修改和随机存取的操作. DOM生成 //判断一下是否存在sdcard if(!Environment.getExternalStora ...

  3. Ubuntu根目录下各文件的功能介绍

    http://jingyan.baidu.com/article/afd8f4de55189c34e286e9e6.html

  4. python3控制路由器--使用requests重启极路由.py

    代码写了相应的注释,以后再写成可以方便调用的模块. 用fiddler抓包可以看到很多HTTP头,经过尝试发现不是都必须的. 'Upgrade-Insecure-Requests':1,#必要项,值为1 ...

  5. Android开发中的ANR问题如何避免?

    在Android中, Activity Manager 和 Window Manager system services 会监控每个程序的运行,当程序出现如下三种种情况的时候就会弹出ANR的提示对话框 ...

  6. poj1845 数论

    //Accepted 204K 16MS //约数和 //n=p1^e1*p2^e2***pk^ek //约数和为:(p1^0+p1^1+..+p1^e1)*(p2^0+p2^1+..+p2^e2)* ...

  7. 04-树5 Complete Binary Search Tree

    这题也是第二次做,本想第一次做时参考的算法会和老师讲的一样,不想老师讲的算法用在这题感觉还不如思雪园友的算法(http://www.cnblogs.com/sixue/archive/2015/04. ...

  8. TCP同步传送数据示例(简洁、清楚)

    转自:http://www.2cto.com/kf/201206/134841.html 本例子写了个简单的TCP数据传送功能.没有使用BinaryWriter,BinaryReader,而是使用Ne ...

  9. 爬虫学习--使用百度api---天气

    #coding:utf-8#version:0.1#note:该即用API能查询指定城市的空气质量指数,但城市数量有限,截止2015年3月26日,只能查到全国161个城市的. import urlli ...

  10. python类的定义和使用

    python中类的声明使用关键词class,可以提供一个可选的父类或者说基类,如果没有合适的基类,那就用object作为基类. 定义格式: class 类名(object): "类的说明文档 ...