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.

https://leetcode.com/problems/minimum-depth-of-binary-tree/

题意就是给定一个二叉树,找出从根节点到叶子节点最低的高度,直接写了个递归,算出所有叶子节点的高度,取最小值。

另外一种很显然就用层次遍历(BFS), 就是按层次遍历这棵树,发现当前节点是叶子节点,就直接返回这个节点的高度。这里有个问题就是怎么记录当前的高度呢?我是这么做的:设置三个变量,upRow,downRow,height,分别代表队列中上一行节点数,下一行节点数,高度,当队列中上一行节点数为0,那么高度+1,把下一行的节点数量(downRow)赋给上一行(upRow),循环不变式是队列非空(!queue.isEmpty())。

Talk is cheap。

import java.util.LinkedList;
import java.util.List; /**
* Created with IntelliJ IDEA.
* User: Blank
* Date: 2015/3/21
*/
public class MinimumDepthofBinaryTree { public int minDepth(TreeNode root) {
int left = Integer.MAX_VALUE, right = Integer.MAX_VALUE;
if (root == null)
return 0;
if (root.right == null && root.left == null)
return 1;
if (root.left != null)
left = minDepth(root.left) + 1;
if (root.right != null)
right = minDepth(root.right) + 1;
return Math.min(left, right);
} public int minDepth2(TreeNode root) {
if (root == null) {
return 0;
}
int upRow = 1, downRow = 0, height = 0;
List<TreeNode> queue = new LinkedList<>();
queue.add(root);
while (!queue.isEmpty()) {
TreeNode node = queue.get(0);
queue.remove(0);
if (node.left == null && node.right == null)
return height + 1;
if (node.left != null) {
queue.add(node.left);
downRow++;
}
if (node.right != null) {
queue.add(node.right);
downRow++;
}
upRow--;
if (upRow == 0) {
height++;
upRow = downRow;
downRow = 0;
}
}
return height;
}
}

Minimum Depth of Binary Tree ——LeetCode的更多相关文章

  1. Minimum Depth of Binary Tree [LeetCode]

    Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...

  2. Minimum Depth of Binary Tree leetcode java

    题目: Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the ...

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

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

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

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

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

  6. LeetCode OJ Minimum Depth of Binary Tree 递归求解

        题目URL:https://leetcode.com/problems/minimum-depth-of-binary-tree/ 111. Minimum Depth of Binary T ...

  7. [Leetcode][JAVA] Minimum Depth of Binary Tree && Balanced Binary Tree && Maximum Depth of Binary Tree

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

  8. 【LeetCode练习题】Minimum Depth of Binary Tree

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

  9. LeetCode My Solution: Minimum Depth of Binary Tree

    Minimum Depth of Binary Tree Total Accepted: 24760 Total Submissions: 83665My Submissions Given a bi ...

随机推荐

  1. 使用EPEL和REMI第三方yum源

    http://dl.fedoraproject.org/pub/epel/ epel-release-latest-.noarch.rpm redhat5 epel-release-latest-.n ...

  2. 进阶篇,第二章:MC与Forge的Event系统

    <基于1.8 Forge的Minecraft mod制作经验分享> 这一章其实才应该是第一章,矿物生成里面用到了Event的一些内容.如果你对之前矿物生成那一章的将算法插入ORE_GEN_ ...

  3. Codeforces 245G Suggested Friends - 交集set_intersection()

    一些人互相是朋友(无向图连一条无向边),现在要添加一个推荐朋友的功能,如果对于x,有y,x与y不是朋友,但是y和x有共同的朋友,并且共同的朋友最多,那么y可以推荐给x.问对于每一个人,有多少人值得推荐 ...

  4. Day11 - Mysql and ORM

    python 之路,Day11 - python mysql and ORM   本节内容 数据库介绍 mysql 数据库安装使用 mysql管理 mysql 数据类型 常用mysql命令 创建数据库 ...

  5. Dij的堆优化

    #include<algorithm> #include<iostream> #include<cstdio> #include<cstring> #i ...

  6. html.day02

    1.链接标签  a <a href=”http://www.baidu.com”></a> <img src=”aaa”/> 一般情况下:  来源  用 src   ...

  7. maven自动部署到tomcat的问题

    最近需要使用Maven将项目自动部署到Tomcat,在网络上也查找了很多文章,内容大同小异,今天打算在这里给自己做一个小总结 参考网址:http://blog.csdn.net/dilaomimi/a ...

  8. Swift 提示 error running playground...

    创建playground之后,我们将得到一个错误提示,Error running playground: Failed to prepare for communication with playgr ...

  9. React 点击删除列表中对应项(React 获取DOM中自定义属性)

    点击删除按钮,删除列表中对应项本来是React比较基础的应用,可是应用情况变得复杂了以后,我还真想了一会儿才搞定. 简化一下应用场景:点击新增按钮,增加一条输入框,点击输入框旁边的按钮,删除该输入框( ...

  10. javascript之attribute 和 property

    首先看看这两个单词的英文释义(来自有道词典).先是property: property ['prɔpəti] n. 性质,性能:财产:所有权 英英释义: any area set aside for ...