本文是在学习中的总结,欢迎转载但请注明出处: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. jQuery – AJAX load() 方法

    jQuery load() 方法 jQuery load() 方法是简单但强大的 AJAX 方法. load() 方法从服务器加载数据,并把返回的数据放入被选元素中. 语法: $(selector). ...

  2. 微信小程序基础之开源项目库汇总

    awesome-github-wechat-weapp 是由OpenDigg整理并维护的微信小程序开源项目库集合.我们会定期同步OpenDigg上的项目到这里,也欢迎各位提交项目给我们. (链接:ht ...

  3. mongo 读分析

    分布式读 读冲突 分布式中数据库有多份数据,各份数据可能存在不一致性. mongo 只会写到primary节点上,理论上来说不会有文档冲突,也就是说数据库中的数据都以primary节点为标准. 但是有 ...

  4. The type org.apache.commons.lang.exception.NestableRuntimeException cannot be resolved.

    最近自己练手写项目时,遇到了这个错,写个文章记录下, The type org.apache.commons.lang.exception.NestableRuntimeException canno ...

  5. 【mybatis深度历险系列】深入浅出mybatis中原始dao的开发和mapper代理开发

    使用Mybatis开发Dao,通常有两个方法,即原始Dao开发方法和Mapper接口开发方法.mybatis在进行dao开发的时候,涉及到三姐妹,分别是SqlSessionFactoryBuilder ...

  6. LuaHotUpdate原理

    LuaHotUpdate原理(金庆的专栏)项目地址:https://github.com/asqbtcupid/lua_hotupdate只更新函数,不更新数据.主页上有个动画演示.限Windows平 ...

  7. Swift如何取得View所属的ViewController

    从VC取得View很容易,但有些情况下我们需要从View反向获取VC. 不过在一些特殊的场合,Cocoa库帮我们想的很周到,比如在自定义View过渡动画的时候: func animateTransit ...

  8. iOS7 CookBook精彩瞬间(一)property、selector细节、__unused

    1.我们常常使用nonatomic,很多人只知道它的效率较高,却不知道其含义,其含义是非线程安全的,也就是说多线程修改时不加锁,可能出现多个线程先后修改而成为脏数据的情况. 2.unsafe_unre ...

  9. quartz 时间设置(定时任务scheduler)

    quartz用来设置定时任务的作业调度程序.在linux的crontab中用到. 格式为: * * * * * * * 其从左到右顺序代表 :[秒] [分] [小时] [日] [月] [周] [年] ...

  10. Android的SeekBar和RateBar的使用-android学习之旅(三十二)

    SeekBar简介 SeekBar允许用户拖动,进行调节经常用于音量调节等方面. android:thumb设置drawable对象来表示拖动的物体. setOnSeekBarChangeListen ...