【LeetCode】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.

递归和非递归,此提比较简单。广度优先遍历即可。关键之处就在于如何保持访问深度。

下面是4种代码:

  1. import java.util.ArrayList;
  2. import java.util.LinkedList;
  3. import java.util.List;
  4. import java.util.Queue;
  5.  
  6. class TreeNode {
  7. int val;
  8. TreeNode left;
  9. TreeNode right;
  10.  
  11. TreeNode(int x) {
  12. val = x;
  13. }
  14. }
  15. public class MinimumDepthofBinaryTree {
  16. /**
  17. * 递归深度遍历
  18. * @param root
  19. * @return
  20. */
  21. public int minDepth1(TreeNode root) {
  22. if(root==null)
  23. return 0;
  24. if(root.left==null&&root.right==null){
  25. return 1;
  26. }
  27. int left=minDepth1(root.left);
  28. int right=minDepth1(root.right);
  29. if(left==0){
  30. return right+1;
  31. }
  32. if(right==0){
  33. return left+1;
  34. }
  35. else{
  36. return Math.min(right, left)+1;
  37. }
  38. }
  39. public int minDepth2(TreeNode root) {
  40. if(root==null)
  41. return 0;
  42. int left=minDepth1(root.left);
  43. int right=minDepth1(root.right);
  44.  
  45. if(left==0&&right==0){
  46. return 1;
  47. }
  48. if(left==0){
  49. right= Integer.MAX_VALUE;
  50. }
  51. if(right==0){
  52. left=Integer.MAX_VALUE;
  53. }
  54.  
  55. return Math.min(right, left)+1;
  56.  
  57. }
  58.  
  59. /**
  60. * 广度优先搜索。一旦发现叶子结点,返回遍历深度。
  61. * @param root
  62. * @return
  63. */
  64. public int minDepth3(TreeNode root) {
  65. if(root==null){
  66. return 0;
  67. }
  68. Queue<TreeNode>queue=new LinkedList<>();
  69. queue.add(root);
  70. int count=queue.size();//用来保存访问当前层次剩余未访问的节点。
  71. int depth=1;//用来保存二叉树的访问深度
  72. while(!queue.isEmpty()){
  73. //广度遍历
  74. TreeNode topNode=queue.poll();
  75. count--;
  76. if(topNode.left!=null){
  77. queue.add(topNode.left);
  78. }
  79. if(topNode.right!=null){
  80. queue.add(topNode.right);
  81. }
  82. //发现叶子节点
  83. if(topNode.left==null&&topNode.right==null){
  84. return depth;
  85. }
  86. //访问一层完毕
  87. if(count==0){
  88. depth++;
  89. count=queue.size();
  90. }
  91.  
  92. }
  93. return 0;
  94. }
  95. /**
  96. * 广度优先搜索。一旦发现叶子结点,返回遍历深度。
  97. * @param root
  98. * @return
  99. */
  100. public int minDepth4(TreeNode root) {
  101. if(root==null){
  102. return 0;
  103. }
  104. List<TreeNode>list=new ArrayList<>();
  105. int count=1;
  106. list.add(root);
  107. while(list.size()!=0){
  108. List<TreeNode>singleList=new ArrayList<>();
  109. for(TreeNode t:list){
  110. if(t.left!=null){
  111. singleList.add(t.left);
  112. }if(t.right!=null){
  113. singleList.add(t.right);
  114. }
  115. if(t.left==null&&t.right==null){
  116. return count;
  117. }
  118. }
  119. count++;
  120. list=singleList;
  121. }
  122. return 0;
  123. }
  124. public static void main(String[] args) {
  125. TreeNode rootNode1 = new TreeNode(1);
  126. TreeNode rootNode2 = new TreeNode(2);
  127. TreeNode rootNode3 = new TreeNode(3);
  128. TreeNode rootNode4 = new TreeNode(4);
  129. TreeNode rootNode5 = new TreeNode(5);
  130. TreeNode rootNode6 = new TreeNode(6);
  131. TreeNode rootNode7 = new TreeNode(7);
  132. rootNode1.left = rootNode2;
  133. rootNode1.right = rootNode3;
  134. rootNode2.left = rootNode4;
  135. rootNode2.right = rootNode5;
  136. rootNode3.left = rootNode6;
  137. rootNode3.right = rootNode7;
  138. MinimumDepthofBinaryTree minimumDepthofBinaryTree=new MinimumDepthofBinaryTree();
  139. System.out.println(minimumDepthofBinaryTree.minDepth4(rootNode1));
  140.  
  141. }
  142.  
  143. }

【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java的更多相关文章

  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] 111. Minimum Depth of Binary Tree ☆(二叉树的最小深度)

    [Leetcode] Maximum and Minimum Depth of Binary Tree 二叉树的最小最大深度 (最小有3种解法) 描述 解析 递归深度优先搜索 当求最大深度时,我们只要 ...

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

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

  5. 111 Minimum Depth of Binary Tree 二叉树的最小深度

    给定一个二叉树,找出其最小深度.最小深度是从根节点到最近叶节点的最短路径的节点数量.详见:https://leetcode.com/problems/minimum-depth-of-binary-t ...

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

  7. LeetCode:111_Minimum Depth of Binary Tree | 二叉树的最小深度 | Easy

    要求:此题正好和Maximum Depth of Binary Tree一题是相反的,即寻找二叉树的最小的深度值:从根节点到最近的叶子节点的距离. 结题思路:和找最大距离不同之处在于:找最小距离要注意 ...

  8. 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 ...

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

随机推荐

  1. table操作:边框-斑马线-多表头-焦点高亮-自动求和

    一.操作table,本例子实现的功能: 1.table等宽边框2.table斑马线3.实现table多表头4.焦点所在行高亮5.自动计算总分 二.效果图 三.代码: <!DOCTYPE html ...

  2. Yii2.0中文开发向导——自定义日志文件写日志

    头部引入log类use yii\log\FileTarget; $time = microtime(true);$log = new FileTarget();$log->logFile = Y ...

  3. yii2.0 控制器方法 视图表单 Form表单处理

    假设我们在ArticleController.php下面的actionForm方法中来处理提交的表单 新建立一个 views/Article/article-form.php文件用来作为输入表单 &l ...

  4. codeforces 55D. Beautiful numbers 数位dp

    题目链接 一个数, 他的所有位上的数都可以被这个数整除, 求出范围内满足条件的数的个数. dp[i][j][k], i表示第i位, j表示前几位的lcm是几, k表示这个数mod2520, 2520是 ...

  5. JAVA并发,线程优先级

    package com.xt.thinks21_2; import java.util.concurrent.ExecutorService; import java.util.concurrent. ...

  6. C功底挑战Java菜鸟入门概念干货(一)

    一.认识Java 1.Java 程序比较特殊,它必须先经过编译,然后再利用解释的方式来运行.  2.Byte-codes 最大的好处是——可越平台运行,可让“一次编写,处处运行”成为可能.  3.使用 ...

  7. UVA 11549 Calculator Conundrum (Floyd判圈算法)

    题意:有个老式计算器,每次只能记住一个数字的前n位.现在输入一个整数k,然后反复平方,一直做下去,能得到的最大数是多少.例如,n=1,k=6,那么一次显示:6,3,9,1... 思路:这个题一定会出现 ...

  8. 2013第四届蓝桥杯决赛Java高职高专组题目以及解法答案

    2013第四届蓝桥杯决赛Java高职高专组题目以及解法答案 不知不觉离决赛都过去一个月了,一直忙于各种事情,都忘记整理一份试题.当作回忆也好. 1. 标题:好好学习 汤姆跟爷爷来中国旅游.一天,他帮助 ...

  9. Android UI设计

    Android UI设计--PopupWindow显示位置设置 摘要: 当点击某个按钮并弹出PopupWindow时,PopupWindow左下角默认与按钮对齐,但是如果PopupWindow是下图的 ...

  10. ssh环境搭建并实现登录功能

    参照了这篇博客,但是里面有些地方进行了更改 http://wenku.baidu.com/link?url=edeegTquV2eR3CJ5-zvNcJbyuq11Afp-lD2Fz2jfmuHhV1 ...