本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/41964249

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.

思路:

(1)题意为求解二叉树最小深度:即为从树根到叶子节点最短路径。

(2)这道题的思路和按层次输出二叉树类似,按层次输出二叉树可以参照http://blog.csdn.net/pistolove/article/details/41929059

(3)为了找到最短路径,在对二叉树进行层次遍历过程中,如果遇到第一个叶子节点,则该叶子节点到根节点的距离即为最短路径。所以只需在对二叉树层次遍历的过程中,判断遍历的节点是否有左右孩子节点,如果没有左右孩子节点,则该节点到根节点之间的距离即为最短路径。

(4)如果能够理解二叉树层次遍历的思想,那么相关的很多题目都能从中得到解答。

(5)希望对你有所帮助。谢谢。

算法代码实现如下所示:

//最小深度
public static int getUndeep(TreeNode root){
	if(root==null) return 0;
	if(root!=null&&root.left==null&&root.right==null) return 1;
	//根节点不为空 默认为1层
	int level = 1;
	LinkedList<TreeNode> list = new LinkedList<TreeNode>();
	list.add(root);
	int first = 0;
	int last = 1;
	while(first<list.size()){
		last = list.size();
		while(first<last){
			if(list.get(first).left!=null){
				list.add(list.get(first).left);
			}
			if(list.get(first).right!=null){
				list.add(list.get(first).right);
			}

			if(list.get(first).left==null && list.get(first).right==null){
				return level++;
			}
			first++;
		}
		level++;
	}
	return level;
}

Leetcode_111_Minimum Depth of Binary Tree的更多相关文章

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

  2. [LeetCode] Maximum Depth of Binary Tree 二叉树的最大深度

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

  3. [LintCode] Maximum Depth of Binary Tree 二叉树的最大深度

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

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

  5. LeetCode:Minimum Depth of Binary Tree,Maximum Depth of Binary Tree

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

  6. LEETCODE —— binary tree [Same Tree] && [Maximum Depth of Binary Tree]

    Same Tree Given two binary trees, write a function to check if they are equal or not. Two binary tre ...

  7. 33. Minimum Depth of Binary Tree && Balanced Binary Tree && Maximum Depth of Binary Tree

    Minimum Depth of Binary Tree OJ: https://oj.leetcode.com/problems/minimum-depth-of-binary-tree/ Give ...

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

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

  9. Leetcode | Minimum/Maximum Depth of Binary Tree

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

随机推荐

  1. Java内存泄漏分析系列之一:使用jstack定位线程堆栈信息

    原文地址:http://www.javatang.com 前一段时间上线的系统升级之后,出现了严重的高CPU的问题,于是开始了一系列的优化处理之中,现在将这个过程做成一个系列的文章. 基本概念 在对J ...

  2. 在查询语句中使用 NOLOCK 和 READPAST

    对于非银行等严格要求事务的行业,搜索记录中出现或者不出现某条记录,都是在可容忍范围内,所以碰到死锁,应该首先考虑,我们业务逻辑是否能容忍出现或者不出现某些记录,而不是寻求对双方都加锁条件下如何解锁的问 ...

  3. 《读书报告 -- Elasticsearch入门 》--简单使用(2)

    <读书报告 – Elasticsearch入门 > ' 第四章 分布式文件存储 这章的主要内容是理解数据如何在分布式系统中存储. 4.1 路由文档到分片 创建一个新文档时,它是如何确定应该 ...

  4. Java中常用缓存Cache机制的实现

    缓存,就是将程序或系统经常要调用的对象存在内存中,一遍其使用时可以快速调用,不必再去创建新的重复的实例. 这样做可以减少系统开销,提高系统效率. 缓存主要可分为二大类: 一.通过文件缓存,顾名思义文件 ...

  5. 有没有最好的学习Angularjs2的视频入门体验?

    Which are the best video tutorials for learning AngularJS 2? 有没有最好的学习Angularjs2的视频入门体验? 翻译来源:https:/ ...

  6. [转]django-registration quickstart

    Basic configuration and use--------------------------- Once installed, you can add django-registrati ...

  7. ActiveMQ + NodeJS + Stomp 极简入门

    前提 安装ActiveMQ和Nodejs 测试步骤 1.执行bin\win32\activemq.bat启动MQ服务 2. 打开http://localhost:8161/admin/topics.j ...

  8. Redis集群教程(Redis cluster tutorial)

    本博文翻译自Redis官网:http://redis.io/topics/cluster-tutorial        本文档以温和的方式介绍Redis集群,不使用复杂的方式来理解分布式系统的概念. ...

  9. Unity UGUI图文混排源码(四) -- 聊天气泡

    这里有同学建议在做聊天气泡时,可以更改为一张图集对应多个Text,这样能节省资源,不过我突然想到每个Text一个图集,可以随时更换图集,这样表情图更丰富一些,于是我就先将现有的聊天demo改为了聊天气 ...

  10. Java学习之控制跳转语句

    控制跳转语句 控制跳转语句: (1)break:中断的意思 A:用在循环和switch语句中,离开此应用场景无意义. B:作用 a:跳出单层循环 b:跳出多层循环,需要标签语句的配合 (2)conti ...