【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. C#控件大小随窗体大小等比例变化

    相信很多博友在开发初次接触学习C# winForm时,当窗体大小变化时,窗体内的控件并没有随着窗体的变化而变化,最近因为一个项目工程的原因,也需要解决这个问题.通过查阅和学习,这个问题得到了解决,或许 ...

  2. Hql中占位符(转)

    在新的Hibernate 4版本中,对于Hql有一点点改变,如果你还是按照以前的方式去编写HQL并且用了以下占位符的方式,就会得到一个警告. 参考资料:https://hibernate.atlass ...

  3. codeforces 633C. Spy Syndrome 2 hash

    题目链接 C. Spy Syndrome 2 time limit per test 2 seconds memory limit per test 256 megabytes input stand ...

  4. Python的maketrans() 方法

    描述 Python maketrans() 方法用于创建字符映射的转换表,对于接受两个参数的最简单的调用方式,第一个参数是字符串,表示需要转换的字符,第二个参数也是字符串表示转换的目标. 注:两个字符 ...

  5. python parse命令行参数

    #!/usr/bin/env python import sys def main(argv): for arg in argv: print arg if __name__ == '__main__ ...

  6. Python中的isinstance函数

    isinstance是Python中的一个内建函数 语法: isinstance(object, classinfo)   如果参数object是classinfo的实例,或者object是class ...

  7. 解决:sudo: parse error in /etc/sudoers near line 24 ...报错

    ubuntu系统下由于添加用户权限的时候直接用的vim对 /etc/sudoers 文件编辑,保存退出的时候,再使用sudo su 等等命令一直报错如下: sudo: parse error in / ...

  8. mobile&nbsp;web&nbsp;手机开发

    1.   -webkit-tap-highlight-color -webkit-tap-highlight-color:rgba(255,255,255,0); 用来把android上点击网页时出现 ...

  9. Noip2013心态调整

    决定成绩的,很多时候可能不是实力,而是心态,一年走来,承受着一次次失败,怀疑,背负着希望与压力,突然发现,只有拥有过,失去过,才可以真正去超越,我希望完成我的梦想,但是唯有放下梦想,才可以走向它. 心 ...

  10. Java程序栈信息文件中的秘密(五)

    最近发现在使用jstack工具在导出Java应用的线程栈时有一个小小的窍门,比如Linux环境上有一个用户为appuser,假如以这个用户启动了一个Java进程B,如果想要导出进程B的线程栈,则必须切 ...