本文是在学习中的总结,欢迎转载但请注明出处: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. ACM Primes

    Write a program to read in a list of integers and determine whether or not each number is prime. A n ...

  2. jQuery 捕获

    jQuery 拥有可操作 HTML 元素和属性的强大方法. jQuery DOM 操作 jQuery 中非常重要的部分,就是操作 DOM 的能力. jQuery 提供一系列与 DOM 相关的方法,这使 ...

  3. 20160219.CCPP体系详解(0029天)

    程序片段(01):ReplaceAll.c 内容概要:ReplaceAll #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #incl ...

  4. springMVC源码分析--AbstractControllerUrlHandlerMapping(六)

    上一篇博客springMVC源码分析--AbstractDetectingUrlHandlerMapping(五)中我们介绍了AbstractDetectingUrlHandlerMapping,其定 ...

  5. C#删除WebBrowser控件的Session

    因最近做一个成绩查询导出的程序,用到webbrowser控件,该查询的网站限制一个会话只能查询3次成绩,而我要查询4000多人的成绩. using System.Runtime.InteropServ ...

  6. 通过grub-install命令把grub安装到u盘

    通过grub-install命令把grub安装到u盘 ①准备一个u盘,容量不限,能有1MB都足够了. ②把u盘格式化(我把u盘格式化成FAT.fat32格式了,最后证明也是成功的).③开启linux系 ...

  7. FORM实现中打开图片,链接,文档(参考自itpub上一篇帖子,整理而来)

    FORM实现中打开图片,链接,文档 参考自itpub上一篇帖子,整理而来 1.添加PL程序库D2kwutil.pll 2.主要实现程序 /*过程参数说明: v_application --打开文件的应 ...

  8. 值集&快速编码(Lookup_code)

    --值集 SELECT ffv.flex_value, ffv.description   FROM fnd_flex_values_vl ffv, fnd_flex_value_sets ffs   ...

  9. gitlab的搭建及问题的解决

    gitlab则是类似于github的一个工具,github无法免费建立私有仓库,并且为了代码安全,于是在内网安装了一个自己实验室的一个git服务器,gitlab有很多依赖,而bitnami制作了一键安 ...

  10. FFmpeg源代码简单分析:avformat_alloc_output_context2()

    ===================================================== FFmpeg的库函数源代码分析文章列表: [架构图] FFmpeg源代码结构图 - 解码 F ...