[LeetCode][Java] Minimum Depth of Binary Tree
题目:
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的更多相关文章
- [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 ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- [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 OJ Minimum Depth of Binary Tree 递归求解
题目URL:https://leetcode.com/problems/minimum-depth-of-binary-tree/ 111. Minimum Depth of Binary T ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- 326 Power of Three 3的幂
给出一个整数,写一个函数来确定这个数是不是3的一个幂.后续挑战:你能不使用循环或者递归完成本题吗? 详见:https://leetcode.com/problems/power-of-three/de ...
- MVC系列学习(九)-DTO的使用
本次学习用的数据库,如下 1.什么是DTO:DataTransferObject 即数据传输对象,服务端的客户端的通信,自动定义个小的实体类,里面只包含我们需要传输的属性 2.不用DTO会有什么问题 ...
- D3.js 力导向图(小气泡围绕中心气泡)
html <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3 ...
- Java 中 父类变量访问子类方法 需要使用 类型转换 (instenceof)关键字 /类型判断/
通过数组元素访问方法的时候只能访问在 Animal中定义的方法,对 于 Tiger类和 Fish中定义的方法时却不能调用,例如语句 animal[2].swim();就是不正确的.当 需要访问这些 ...
- <stddef.h>
Common definitions 定义类型: ptrdiff_t 两指针相减的结果,signed integer size_t sizeof操作符的结果,unsigned integer max_ ...
- Rsync 传输不需要输入密码
1.背景 1) 一个作为服务器端:VM3(IP: 3.9.8.151) 2) 一个作为客户端:VM2(IP: 3.9.8.157) 3) 服务器端和客户端网络 ...
- Redis系列(一)StackExchange.Redis的使用
Redis系列(一)StackExchange.Redis的使用 一.DLL安装 用NuGet搜索StackExchange.Redis,然后下载就可以. ConnectionMultiplexer对 ...
- dos2unix xxx.sh
前几天写了一个Linux的自动化脚本,没有使用变量,就是一些Linux命令的集合 今天试着把一些相同的字段提出来用变量表示,然后在Linux里运行,就发现一直在报错: : command not fo ...
- C 语言常用方法技巧
C语言常用方法技巧 *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !impor ...
- Python 之动态添加属性以及方法
import types class Person(object): def __init__(self, newName, newAge): self.name = newName self.age ...