【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种代码:

 import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue; class TreeNode {
int val;
TreeNode left;
TreeNode right; TreeNode(int x) {
val = x;
}
}
public class MinimumDepthofBinaryTree {
/**
* 递归深度遍历
* @param root
* @return
*/
public int minDepth1(TreeNode root) {
if(root==null)
return 0;
if(root.left==null&&root.right==null){
return 1;
}
int left=minDepth1(root.left);
int right=minDepth1(root.right);
if(left==0){
return right+1;
}
if(right==0){
return left+1;
}
else{
return Math.min(right, left)+1;
}
}
public int minDepth2(TreeNode root) {
if(root==null)
return 0;
int left=minDepth1(root.left);
int right=minDepth1(root.right); if(left==0&&right==0){
return 1;
}
if(left==0){
right= Integer.MAX_VALUE;
}
if(right==0){
left=Integer.MAX_VALUE;
} return Math.min(right, left)+1; } /**
* 广度优先搜索。一旦发现叶子结点,返回遍历深度。
* @param root
* @return
*/
public int minDepth3(TreeNode root) {
if(root==null){
return 0;
}
Queue<TreeNode>queue=new LinkedList<>();
queue.add(root);
int count=queue.size();//用来保存访问当前层次剩余未访问的节点。
int depth=1;//用来保存二叉树的访问深度
while(!queue.isEmpty()){
//广度遍历
TreeNode topNode=queue.poll();
count--;
if(topNode.left!=null){
queue.add(topNode.left);
}
if(topNode.right!=null){
queue.add(topNode.right);
}
//发现叶子节点
if(topNode.left==null&&topNode.right==null){
return depth;
}
//访问一层完毕
if(count==0){
depth++;
count=queue.size();
} }
return 0;
}
/**
* 广度优先搜索。一旦发现叶子结点,返回遍历深度。
* @param root
* @return
*/
public int minDepth4(TreeNode root) {
if(root==null){
return 0;
}
List<TreeNode>list=new ArrayList<>();
int count=1;
list.add(root);
while(list.size()!=0){
List<TreeNode>singleList=new ArrayList<>();
for(TreeNode t:list){
if(t.left!=null){
singleList.add(t.left);
}if(t.right!=null){
singleList.add(t.right);
}
if(t.left==null&&t.right==null){
return count;
}
}
count++;
list=singleList;
}
return 0;
}
public static void main(String[] args) {
TreeNode rootNode1 = new TreeNode(1);
TreeNode rootNode2 = new TreeNode(2);
TreeNode rootNode3 = new TreeNode(3);
TreeNode rootNode4 = new TreeNode(4);
TreeNode rootNode5 = new TreeNode(5);
TreeNode rootNode6 = new TreeNode(6);
TreeNode rootNode7 = new TreeNode(7);
rootNode1.left = rootNode2;
rootNode1.right = rootNode3;
rootNode2.left = rootNode4;
rootNode2.right = rootNode5;
rootNode3.left = rootNode6;
rootNode3.right = rootNode7;
MinimumDepthofBinaryTree minimumDepthofBinaryTree=new MinimumDepthofBinaryTree();
System.out.println(minimumDepthofBinaryTree.minDepth4(rootNode1)); } }

【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. asp.net 下载文件(图片、word、excel等)

    string filePath = Server.MapPath("~/excel.xlsx"); if (File.Exists(filePath)) { FileStream ...

  2. linq中的GroupBy总结

    1.简单形式: var q = from p in db.Products group p by p.CategoryID into g select g; 语句描述:Linq使用Group By按C ...

  3. BZOJ 2245: [SDOI2011]工作安排( 费用流 )

    费用流模板题..限制一下不同愤怒值的工作数就可以了. ------------------------------------------------------------------------- ...

  4. Hadoop学习之shuffle过程

    转自:http://langyu.iteye.com/blog/992916,多谢分享,学习Hadopp性能调优的可以多关注一下 Shuffle过程是MapReduce的核心,也被称为奇迹发生的地方, ...

  5. mina学习资料整合

    最好的资料当然是官方文档:https://mina.apache.org/mina-project/userguide/user-guide-toc.html 官方文档,配合源码中的example例子 ...

  6. 老旧Webkit浏览器行内元素0间距问题

    有时我们希望display:inline-block的元素之间的天衣无缝.紧密相依,比如说如下的情情形: 一般情况下我们使用如下代码可以实现: .pageNav { font-size:; text- ...

  7. android-数据持久化

    动态文件 String FILE_NAME = "tempfile.tmp"; try { //读 FileInputStream fis = openFileInput(FILE ...

  8. 浅谈qt 布局器

    在一个颜值当道的今天,无论买衣服,买车还是追星,颜值的高低已经变成了大家最看重的(不管男性女性都一样,千万别和我说你不是):而对于程序猿来说,开发一款软件,不再只注重逻辑和稳定性,美观和用户友好性也是 ...

  9. rsyslog 基本组成

    Facility 定义日志消息的来源,以方便对日志进行分类,facility 有以下几种: --kern 内核消息 --user 用户级消息 --mail 邮件系统消息 --daemon 系统服务消息 ...

  10. CCNA实验(6) -- VLAN & SPT

    交换机的作用主要有两个:1.维护CAM(ContextAddress Memory)表,该表是MAC地址和交换机端口的映射表2.根据CAM进行数据帧的转发 交换机对数据帧的处理有三种:1.Forwar ...