111 Minimum Depth of Binary Tree 二叉树的最小深度
给定一个二叉树,找出其最小深度。
最小深度是从根节点到最近叶节点的最短路径的节点数量。
详见: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 二叉树的最小深度的更多相关文章
- [LeetCode] 111. Minimum Depth of Binary Tree ☆(二叉树的最小深度)
[Leetcode] Maximum and Minimum Depth of Binary Tree 二叉树的最小最大深度 (最小有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 ...
- 【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] 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] 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 ...
- Leetcode 111 Minimum Depth of Binary Tree 二叉树
找出最短的从叶子到根的路径长 可以回忆Maximum Depth of Binary Tree的写法,只不过在!root,我把它改成了10000000,还有max函数改成了min函数,最后的值如果是1 ...
- LeetCode:111_Minimum Depth of Binary Tree | 二叉树的最小深度 | Easy
要求:此题正好和Maximum Depth of Binary Tree一题是相反的,即寻找二叉树的最小的深度值:从根节点到最近的叶子节点的距离. 结题思路:和找最大距离不同之处在于:找最小距离要注意 ...
- 【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 ...
- (二叉树 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 ...
随机推荐
- 使用XMLHttpRequest
请求种类 通过XMLHttpRequest的请求可以通过同步和异步的方式获取数据,请求的种类在XMLHttpRequest的open()方法的第三三个可选参数async设置.如果这个参数是true或者 ...
- Android四种启动模式
四种启动模式 standard(默认) singleTop singleTast singleInstance standard(默认) 系统默认的启动模式. Android是使用返回栈来管理活动的, ...
- 23.java方法的深入
深入: public class MethodTest05{ public static void main(String[] args){ int i=m1(ture); System.out.pr ...
- TModJS:使用tmodjs
ylbtech-TModJS:使用tmodjs 1.返回顶部 1. 1.安装 npm install -g tmodjs 2.配置 我的模板都放在tpl文件夹中,htmls用于存放模板页面,每一个后缀 ...
- sqlserver 截取字符串
**/*******/*****/1399/* 我要取第3个'/'与第4个'/'中的内容,就是1399 create table ta( col varchar(100)) insert ta sel ...
- sqlserver:rank() over()函数
先前在oracle数据库中接触过over()函数的一系列使用,但是在实际的sql开发中并没有太多的使用,这次在sqlserver的sql拼写中重新遇到这个函数,故结合网上的例子和项目中实际中使用,将其 ...
- http://blog.sina.com.cn/s/blog_6145ed810102vr8k.html
http://blog.sina.com.cn/s/blog_6145ed810102vr8k.html
- CNN相关资料
转子http://blog.csdn.net/qianqing13579/article/details/71076261 前言 入职之后,逐渐转到深度学习方向.很早就打算写深度学习相关博客了,但是由 ...
- 1.5 Hive初步使用和安装MySQL
一.HQL初步试用 1.创建一个student表 #创建一个student表 hive> create table student(id int, name string) ROW FORMAT ...
- 坑暗花明:又遇 .NET Core 中 System.Data.SqlClient 查询缓慢的问题
之前发布过一篇博文 下单快发货慢:一个 JOIN SQL 引起 SqlClient 读取数据慢的奇特问题,当时遇到的问题是从 SQL Server 2008 R2 中查询获取 100 条记录竟然耗时 ...