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. AngularJS结构简介

    AngularJS是MVC架构,M是C里面的属性-值,C是js的class,V是DOM 各个关键特性的结构如下图所示: http://my.oschina.net/tommyfok/blog/2970 ...

  2. Android中findViewById()获取EditText 空指针问题

    因为EditText editText = (EditText)layout.findViewById(R.id.input_content);是从Dialog对话框布局layout中寻找ID为inp ...

  3. 支持单色条码图像生成的条形码控件Barcode Professional

    Barcode Professional for .NET Windows Forms条形码控件是一款灵活和强大的.NET组件(.NET DLL 类库),它让您轻松地添加条码生成和打印功能到您的.NE ...

  4. C/C++中函数参数传递详解(一)

    *在定义时使用代表指针类型,其他情况代表取内容.&在定义时使用代表引用(别名),在其他情况代表取地址 在编写个人函数的时候,你将会受到C++中的一条基本的原则的限制:在默认的情况下,变量只能以 ...

  5. .net 小技巧

    简单提示效果: <input runat="server" type="text" id="SelPerson" value=&quo ...

  6. 在Hadoop集群中添加机器和删除机器

    本文转自:http://www.cnblogs.com/gpcuster/archive/2011/04/12/2013411.html 无论是在Hadoop集群中添加机器和删除机器,都无需停机,整个 ...

  7. python解无忧公主数学题108

    """ python解无忧公主数学题108回文.py 题目来源: http://mp.weixin.qq.com/s?__biz=MzI5ODEwMDQyNw==& ...

  8. Reachability判断网络是否连接

    类似于一个网络状况的探针. [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(reachabili ...

  9. java语法学习问题总结

    No.1:EnumTest No.2:Addition 在此程序中,学习了将文本框调用出来,文本框输入的数据都是String类型,所以用于计算时需要先进行转型,然后计算. No.3:TestDoubl ...

  10. VBS_DO...Loop

    循环用于重复执行一组语句.循环可分为三类:一类在条件变为 False 之前重复执行语句,一类在条件变为 True 之前重复执行语句,另一类按照指定的次数重复执行语句. 在 VBScript 中可使用下 ...