本文是在学习中的总结,欢迎转载但请注明出处: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)希望对你有所帮助。谢谢。

算法代码实现如下所示:

  1. //最小深度
  2. public static int getUndeep(TreeNode root){
  3. if(root==null) return 0;
  4. if(root!=null&&root.left==null&&root.right==null) return 1;
  5. //根节点不为空 默认为1层
  6. int level = 1;
  7. LinkedList<TreeNode> list = new LinkedList<TreeNode>();
  8. list.add(root);
  9. int first = 0;
  10. int last = 1;
  11. while(first<list.size()){
  12. last = list.size();
  13. while(first<last){
  14. if(list.get(first).left!=null){
  15. list.add(list.get(first).left);
  16. }
  17. if(list.get(first).right!=null){
  18. list.add(list.get(first).right);
  19. }
  20.  
  21. if(list.get(first).left==null && list.get(first).right==null){
  22. return level++;
  23. }
  24. first++;
  25. }
  26. level++;
  27. }
  28. return level;
  29. }

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. k8s Kubernetes v1.10

    #转移页面 http://www.cnblogs.com/elvi/p/8976305.html

  2. 实验与作业(Python)-02 Python函数入门与温度转换程序

    截止日期 实验目标 学会定义函数,使用函数.学会导入在某个文件中定义的函数. input获得值,然后通过eval或者int.float将其转换为相应的类型. 学会使用列表:访问列表.append.遍历 ...

  3. API得到Windows版本

    API得到Windows版本 /** * Windows Version * https://msdn.microsoft.com/en-us/library/windows/desktop/dn48 ...

  4. 安卓高级 WebView的使用到 js交互

    我们先来学习 怎么使用再到用js和安卓源生方法交互 WebView简单使用 此部分转载并做了补充 原博客 原因:比较简单不是很想在写,我只要写js交互部分 WebView可以使得网页轻松的内嵌到app ...

  5. 带有进度条的WebView

    带有进度条的WebView 本篇继于WebView的使用 效果图 自定义一个带有进度条的WebView package com.kongqw.kbox.view; import android.con ...

  6. Swift中String和NSString的一个不同之处

    我们知道在Swift中String和NSString是可以互相转换使用的-额-应该是在绝大数情况下可以互相转换使用.在某些情况下可能还有一丝丝略微的差别:比如在涉及到处理字符串中字符索引的时候. 我们 ...

  7. 浅析深度学习mini_batch的BP反传算法

    在深度学习中,如果我们已经定义了网络,输入,以及输出,那么接下来就是损失函数,优化策略,以及一般由框架完成的BP反传.这篇博文我们主要探讨一下深度的BP反传算法(以梯度下降为例),尤其是mini_ba ...

  8. boost::asio::spawn 将一统C++网络库

    boost::asio::spawn 将一统C++网络库(金庆的专栏)boost::asio::spawn()创建一个协程,使C++网络编程大大简化,个人认为这使得 asio 成为C++首选网络库.b ...

  9. JAVA通过继承Thread来创建线程

    创建一个线程的第二种方法是创建一个新的类,该类继承Thread类,然后创建一个该类的实例. 继承类必须重写run()方法,该方法是新线程的入口点.它也必须调用start()方法才能执行. 实例 // ...

  10. 一个 Linux 上分析死锁的简单方法

    简介 死锁 (deallocks): 是指两个或两个以上的进程(线程)在执行过程中,因争夺资源而造成的一种互相等待的现象,若无外力作用,它们都将无法推进下去.此时称系统处于死锁状态或系统产生了死锁,这 ...