给定一个二叉树,找出其最小深度。
最小深度是从根节点到最近叶节点的最短路径的节点数量。
详见: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. sublime text3 3176 注册码 License

    注册码 sgbteam Single User License EA7E-1153259 8891CBB9 F1513E4F 1A3405C1 A865D53F 115F202E 7B91AB2D 0 ...

  2. codeforces A. Fox and Box Accumulation 解题报告

    题目链接:http://codeforces.com/problemset/problem/388/A 题目意思:有 n 个 boxes,每个box 有相同的 size 和 weight,但是stre ...

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

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

  4. FFmpeg常用命令 (一)

    常用参数说明: 主要参数:-i 设定输入流-f 设定输出格式-ss 开始时间视频参数:-b 设定视频流量,默认为200Kbit/s-r 设定帧速率,默认为25-s 设定画面的宽与高-aspect 设定 ...

  5. 如何用Adb连接Android手机 & unable to connect to 192.168.1.100:5555的原因和解决方法

    利用adb来连接手机, 有两种方式: 1, wifi 2, usb. 1. 通过wifi, 利用adb来连接手机. 在pc的cmd中输入命令: adb connect 192.168.1.100 其中 ...

  6. UVA11624(bfs最短路)

    Joe works in a maze. Unfortunately, portions of the maze have caught on fire, and the owner of the m ...

  7. 抓屏工具 faststone capture

    百度百科 http://baike.baidu.com/link?url=te51CfOKYIEmqT1jsyRwcB8Pnals5xQ8nUXk6trvBPGSJRBO5G7BEZL7cYQxmx8 ...

  8. svn图标更新缓慢

    TSVNCache.exe:在进程管理里有一个Tsvncache.exe这个进程会不定时的对svn目录和非svn目录进行扫描,会造成一定的资源消耗. 此页面允许你选择TSVN为哪些条目显示图标覆盖.选 ...

  9. 优化jQuery选择器

    优化jQuery选择器 选择优化比以前更加重要,因为越来越多的浏览器实现了queryselectorall()并承担了将jQuery选择器转移到浏览器的责任.记住这些小技巧可以让你轻松突破学习选择器时 ...

  10. mysql引擎问题研究

    mysql引擎问题研究 数据库引擎 缺省情况下,MYSQL支持三个引擎:ISAM,MYISAM和HEAP.还存在MYSQL+API的引擎例如InnoDB. 数据库引擎特点 ISAM:执行读取操作速度很 ...