给定一个二叉树,找出其最小深度。
最小深度是从根节点到最近叶节点的最短路径的节点数量。
详见:https://leetcode.com/problems/minimum-depth-of-binary-tree/description/

Java实现:

递归实现:

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public int minDepth(TreeNode root) {
if(root==null){
return 0;
}
int minLeft=minDepth(root.left);
int minRight=minDepth(root.right);
if(minLeft==0||minRight==0){
return minLeft>=minRight?minLeft+1:minRight+1;
}
return Math.min(minLeft,minRight)+1;
}
}

非递归实现:

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public int minDepth(TreeNode root) {
if(root==null){
return 0;
}
LinkedList<TreeNode> que=new LinkedList<TreeNode>();
que.offer(root);
int node=1;
int level=1;
while(!que.isEmpty()){
root=que.poll();
--node;
if(root.left==null&&root.right==null){
return level;
}
if(root.left!=null){
que.offer(root.left);
}
if(root.right!=null){
que.offer(root.right);
}
if(node==0){
node=que.size();
++level;
}
}
return level;
}
}

111 Minimum Depth of Binary Tree 二叉树的最小深度的更多相关文章

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

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

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

  3. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  4. [LeetCode] 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. [Leetcode] The minimum depth of binary tree二叉树的最小深度

    Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...

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

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

  7. LeetCode:111_Minimum Depth of Binary Tree | 二叉树的最小深度 | Easy

    要求:此题正好和Maximum Depth of Binary Tree一题是相反的,即寻找二叉树的最小的深度值:从根节点到最近的叶子节点的距离. 结题思路:和找最大距离不同之处在于:找最小距离要注意 ...

  8. 【LeetCode】111. Minimum Depth of Binary Tree (2 solutions)

    Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth is the n ...

  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. ARCGIS 发布TIF,金字塔文件是否Server自动生成。

    经过发布一个TIF带OVR的服务TIF_OVR, 和一个不带金字塔的TIF服务TIF_WITHOUT_OVR. 证实,在..\arcgisserver\directories\arcgissystem ...

  2. android中获取包名,类名

    LogUtil.i("getPackageName()=" + getPackageName()); //Context类 LogUtil.i("getClass().g ...

  3. the art of seo(chapter one)

    preface:Andy Johns (@ibringtraffic):growth strategist@Wealthfront ***1.Search Reflecting Consciousne ...

  4. CNN卷积神经网络_深度残差网络 ResNet——解决神经网络过深反而引起误差增加的根本问题,Highway NetWork 则允许保留一定比例的原始输入 x。(这种思想在inception模型也有,例如卷积是concat并行,而不是串行)这样前面一层的信息,有一定比例可以不经过矩阵乘法和非线性变换,直接传输到下一层,仿佛一条信息高速公路,因此得名Highway Network

    from:https://blog.csdn.net/diamonjoy_zone/article/details/70904212 环境:Win8.1 TensorFlow1.0.1 软件:Anac ...

  5. QQ通信原理

    转载自http://blog.csdn.net/li_xiao_ming/article/details/8106857 下面有4个基本的问答: 问题一:为什么只要可以连上互联网的计算机都可以用QQ相 ...

  6. Java 高阶 —— try/catch

    // try catch 在 for 循环外 try { for(int i = 0; i < max; i++) { String myString = ...; float myNum = ...

  7. codevs 3027线段覆盖2

    传送门 3027 线段覆盖 2  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold   题目描述 Description 数轴上有n条线段,线段的两端都是整数坐标, ...

  8. 优化 SQL Server CPU 性能

    本文將探討在使用SQL Server時有那些原因可能會造成過度消耗CPU資源,若CPU使用率管理不善或過度使用CPU資源的話,可能會對SQL Server有明顯的影響,建議您需要增加或更換CPU.. ...

  9. JS获取元素的offsetTop,offsetLeft等相关属性

    1. obj.clientWidth //获取元素的宽度 obj.clientHeight //元素的高度 obj.offsetLeft //元素相对于父元素的left obj.offsetTop / ...

  10. json : json数据解析(一)

    在项目中经常用到json格式的数据转换与解析,先前写过一些小例子,现在整理下,以备后用和帮助后来者. 言归正传: 使用到的jar包 :json-lib-2.4-jdk15.jar,当然你也可以用自己版 ...