[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 ...
随机推荐
- UNIX环境高级编程--8. 进程控制
进程控制进程标识: 每一个进程都有一个非负整型表示的唯一进程ID.虽然唯一,但是ID可以复用.当一个进程结束后,其进程ID会被延迟复用. ID=0的进程通常是调度进程,常被称作交换进程(s ...
- Spring.net(v1.3.2) 官网API-第一章 前言
虽然有很好的工具和技术,但是开发软件应用仍然是很困难的.Spring为构建企业级应用提供了一个轻量级的解决方案,Spring提供了一个统一的.透明的方式去配置你的应用,和将AOP集成到你的软件中.Sp ...
- pengyue-form 模块 dropdown 关系联动
<script> window.onload=function() { var school= document.getElementById("dnn_ctr5973_View ...
- mvc EF 出现异常, 能提示出那个字段出现问题
} catch (DbEntityValidationException ex) { message = "添加异常"; desc = " {" + ex.Me ...
- golang bytes 包
类型 []byte 的切片十分常见,Go 语言有一个 bytes 包专门用来解决这种类型的操作方法. bytes 包和字符串包十分类似.而且它还包含一个十分有用的类型 Buffer: import & ...
- Python编码格式导致的csv读取错误
Python编码格式导致的csv读取错误(pandas.read_csv) 本文记录python小白我今天遇到的这两个问题(csv.reader和pandas.csv_read): pandas模块“ ...
- 3.Linux的远程管理及网络下载
3.1 Linux的远程管理 3.1.1 远程管理概述 什么是远程管理: 1.为什么需要远程管理: 服务器通常是Linux系统,而服务器不可能一直在身边,所以就需要远程来操作服务器 企业中通常需要集群 ...
- php观察折模式
<?php class Paper{ private $_observers = array(); public function register($sub){ $this->_obse ...
- Redis防护建议
1.Redis本身防护 (1)不要使用默认端口(6379) (2)增加Redis用户名和密码 (3)在Redis绑定指定IP访问(位置配置文件[redis.config]中的bind节点)2.L ...
- JAVA中对事物的理解
1.事物是一组操作数据的集合动作 (场景:再开发的时候相信大家都遇到主表和子表的插入问题,当主表插入成功时,子表没有插入成功,这时候我们就要把主表的数据回滚,这个时候我们就要用到事物了) 2.一组处理 ...