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

Java实现:

递归实现:

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public int minDepth(TreeNode root) {
if(root==null){
return 0;
}
int minLeft=minDepth(root.left);
int minRight=minDepth(root.right);
if(minLeft==0||minRight==0){
return minLeft>=minRight?minLeft+1:minRight+1;
}
return Math.min(minLeft,minRight)+1;
}
}

非递归实现:

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public int minDepth(TreeNode root) {
if(root==null){
return 0;
}
LinkedList<TreeNode> que=new LinkedList<TreeNode>();
que.offer(root);
int node=1;
int level=1;
while(!que.isEmpty()){
root=que.poll();
--node;
if(root.left==null&&root.right==null){
return level;
}
if(root.left!=null){
que.offer(root.left);
}
if(root.right!=null){
que.offer(root.right);
}
if(node==0){
node=que.size();
++level;
}
}
return level;
}
}

111 Minimum Depth of Binary Tree 二叉树的最小深度的更多相关文章

  1. [LeetCode] 111. Minimum Depth of Binary Tree ☆(二叉树的最小深度)

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

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

  3. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

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

  6. Leetcode 111 Minimum Depth of Binary Tree 二叉树

    找出最短的从叶子到根的路径长 可以回忆Maximum Depth of Binary Tree的写法,只不过在!root,我把它改成了10000000,还有max函数改成了min函数,最后的值如果是1 ...

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

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

  8. 【LeetCode】111. Minimum Depth of Binary Tree (2 solutions)

    Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth is the n ...

  9. (二叉树 BFS DFS) 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 ...

随机推荐

  1. 分布式session之token解决方案实现

    基于令牌(Token)方式实现Session解决方案,因为Session本身就是分布式共享连接 用token代替session 废话不多说,看项目: pom.xml <project xmlns ...

  2. Python作业之用户管理

    作业 流程图没有画,懒,不想画 readme没有写,懒,不想写.看注释吧233333 #! /usr/bin/env python # -*- coding: utf-8 -*- # __author ...

  3. Dom4j 操作文件,文件相对路径的问题

    System.out.println("xml路径:"+ServletActionContext.getServletContext().getRealPath("/zx ...

  4. Swift类型转换

    关于「类型转换」(Type Casting),<The Swift Programming Language>描述如下: Type casting is a way to check th ...

  5. MongoDB 3.2复制集单节点部署(四)

    MongoDB在单节点中也可以做复制集,但是仅限于测试实验,最大的好处就是部署方便快速,可以随便添加新节点,节省资源.在这里我使用的是MongoDB 3.2版本进行复制集实验(但MongoDB配置文件 ...

  6. Java并发实现线程阻塞原语LockSupport

    LockSupport 和 CAS 是Java并发包中很多并发工具控制机制的基础,它们底层其实都是依赖Unsafe实现.LockSupport是用来创建锁和其他同步类的基本线程阻塞原语. 1.Lock ...

  7. 洛谷 1079 Vigenère 密码——模拟水题

    题目:https://www.luogu.org/problemnew/show/P1079 大水题. #include<iostream> #include<cstdio> ...

  8. vue-touchjs

    支持vue2.0的面向指令的touch指令,基于touchjs(原百度实现的移动端手势库) vue-touchjs支持三种stopPropagation的方式: 1 .stop修饰符 2  事件han ...

  9. webpack 知识点总结

    1 webpack 的不同点: 1.1 实现代码分割 1.2 loaders 1.3 plugin system 2 webpack 打包css,需要使用css-loaders 使webpack识别c ...

  10. Windows 下有什么软件能够极大地提高工作效率

    Windows 下有什么软件能够极大地提高工作效率?修改 可以推荐一些好的应用或者有趣的程序,能提升工作效率或者能让人眼前一亮的.修改 举报1 条评论 分享 • 邀请回答   按票数排序按时间排序 2 ...