【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 }

转载于:https://www.cnblogs.com/hitkb/p/4243664.html

【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java相关推荐

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

    给定一个二叉树,找出其最小深度. 最小深度是从根节点到最近叶子节点的最短路径上的节点数量. 说明: 叶子节点是指没有子节点的节点. 示例: 给定二叉树 [3,9,20,null,null,15,7], ...

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

  3. leetcode - Minimum Depth of Binary Tree

    题目:Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth is th ...

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

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

  6. LeetCode | Minimum Depth of Binary Tree

    题目:给定一个二叉树,找到其最小深度.最小深度是从根节点到最近叶节点的最短路径的节点数. 1 /** 2 * Definition for binary tree 3 * public class T ...

  7. leetcode:Minimum Depth of Binary Tree【Python版】

    1.类中递归调用添加self: 2.root为None,返回0 3.root不为None,root左右孩子为None,返回1 4.返回l和r最小深度,l和r初始为极大值: 1 # Definition ...

  8. leetcode --Minimum Depth of Binary Tree

    找最短的到叶节点的长度: 一考虑广度优先搜索(使用队列,不用 recursive) class Solution { public:int minDepth(TreeNode* root) {if(! ...

  9. LeetCode 545. Boundary of Binary Tree 二叉树边界

    LeetCode 545. Boundary of Binary Tree 二叉树边界 Given a binary tree, return the values of its boundary i ...

最新文章

  1. Android 知识点梳理
  2. C及C++中typedef的简单使用指南
  3. 使用HttpHandler实现图片防盗链
  4. libev源码分析--常用的watcher
  5. c++代码整洁之道pdf_别再问如何用python提取PDF内容了
  6. 为什么选择springcloud微服务架构
  7. Smali动态调试方法
  8. gRPC编译和安装——Linux版
  9. BLE芯片商总结和市场趋势分析【选型使用,建议收藏】
  10. 三国战纪2 ,西游2的FBA 移植攻略!
  11. C#服务端如何获取外网IP
  12. discuz定时采集批量自动发帖
  13. CJB的大作 - 乱搞
  14. 诈骗短信报警12110
  15. web前端移动端课程之canvas教程系列
  16. Android Studio 修改 Java 语言版本到 1.8
  17. 婚庆行业小程序标准版
  18. opencv-python读取摄像头视频流保存为视频
  19. RHCSA 2022/10/14
  20. 计算机英语讲课笔记08

热门文章

  1. html读取servlet,简单html与servlet交互(HTML利用servlet读取txt)
  2. java上机作业要注意什么_Java第八次上机作业
  3. 计算机上课创意互动游戏初中,16个课前热身小游戏:让每一堂课都充满新鲜感...
  4. 蓝桥杯单片机stc15f2k61s2矩阵按键中断扫描代码
  5. linux脚本安装gcc,在Linux系统下不需要编译安装GCC9,有仓库安装模板脚本
  6. ENSP配置 实例八 三层交换机DHCP配置加VLAN划分实验
  7. python实例 91,92,93,94
  8. Css内边距与外边距
  9. php cli 编程,php-cli下编程如何分层架构、面向对象、统一入口文件?
  10. python2.7 pyqt4创建qtapp_python-2.7 – 向TabWidget pyqt4添加加号按钮