题目:

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.

题意:

给定一棵二叉树。返回它的最小高度。

最小高度是指从根节点到近期的叶子节点的最短路径中的节点的数目。

算法分析:

* 借助堆

* 类似《Binary Tree Level
Order Traversal
》中的算法

* 出现下一层无自带的情况,马上退出,返回该层的层数

AC代码:

/**
* 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)
{
int index=0;
boolean flag=false;
ArrayList<ArrayList<Integer>> res=new ArrayList<ArrayList<Integer>>();
ArrayList<Integer> list= new ArrayList<Integer>();
LinkedList<TreeNode> que = new LinkedList<TreeNode>(); if (root==null) return 0;
que.add(root);
while(que!=null)
{
TreeNode tem;
int size=que.size();
list.clear();
for(int i=0;i<size;i++)
{
tem=que.poll();
list.add(tem.val);
if(tem.left==null&&tem.right==null)
{
flag=true;//仅仅要出现父节点无子节点的情况。就直接跳出,统计size
break;
}
if(tem.left!=null) que.add(tem.left);
if(tem.right!=null) que.add(tem.right);
}
res.add(new ArrayList<Integer>(list));
if(flag) return res.size();
else index=res.size();
} return index;
}
}

[LeetCode][Java] Minimum Depth of Binary Tree的更多相关文章

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

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

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

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

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

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

  5. LeetCode OJ Minimum Depth of Binary Tree 递归求解

        题目URL:https://leetcode.com/problems/minimum-depth-of-binary-tree/ 111. Minimum Depth of Binary T ...

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

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

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

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

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

随机推荐

  1. [转]Linux下/proc目录简介

    1. /proc目录Linux 内核提供了一种通过 /proc 文件系统,在运行时访问内核内部数据结构.改变内核设置的机制.proc文件系统是一个伪文件系统,它只存在内存当中,而不占用外存空间.它以文 ...

  2. OpenCV绘制检测结果

    OpenCV绘制检测结果 opencv  rtcp  timestamp  一.介绍 由于在验证阶段,使用FPGA时我们的算法检测速度很慢,没法直接在主流上进行绘图,否则的话,主流就要等待算法很久才能 ...

  3. 【PostgreSQL-9.6.3】分区表

    PostgreSQL中的分区表是通过表继承来实现的(表继承博客http://www.cnblogs.com/NextAction/p/7366607.html).创建分区表的步骤如下: (1)创建“父 ...

  4. SQL基本操作——select into与临时表

    SELECT INTO 语句从一个表中选取数据,然后把数据插入另一个表中,常用于创建表的备份复件或者用于对记录进行存档. --制作 "Persons" 表的备份复件: SELECT ...

  5. div的浮动、清除浮动和布局

    总结: 1.无序列表去除前面的小点点:list-style-type: none; 2.设置左浮动的间距. 外边距:margin :如果设定4个值就是,上右下左的顺序设置 如果设置3个值,那么左和右边 ...

  6. LINUX C: 获取本地指定网卡的IP地址

    #include <stdio.h> #include <stdlib.h> #include <string.h> #include <errno.h> ...

  7. JavaScript day3(数据类型)

    数据类型(data type) JavaScript提供七种不同的数据类型(data types),它们是string(字符串), symbol(符号), number(数字), undefined( ...

  8. 基础命令history

    history   记录历史命令 环境变量: HISTSIZE: 命令历史记录的条数: HISTFILE:  命令历史记录的文件,~/.bash_history: HISTFILESIZE: 命令历史 ...

  9. 【[Offer收割]编程练习赛13 B】最大子矩阵(自己的思路)

    [题目链接]:http://hihocoder.com/contest/offers13/problem/2 [题意] [题解] 算出1..250*250这些数字每个数字的所有因子(成对的那种,即x* ...

  10. ZooKeeper学习总结(2)——ZooKeeper开源Java客户端ZkClient使用

    zkclient是zookeeper的Java客户端.它让Zookeeper API 使用起来更简单:它非常方便订阅各种事件并自动重新绑定事件(会话建立.节点修改.节点删除.子节点变更等):它提供了s ...