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. 安全框架Shiro和Spring Security比较

    Shiro 首先Shiro较之 Spring Security,Shiro在保持强大功能的同时,还在简单性和灵活性方面拥有巨大优势. Shiro是一个强大而灵活的开源安全框架,能够非常清晰的处理认证. ...

  2. 安卓扫码:简单的ZXing使用记录

    ZXing是Google提供的条形码.二维码等的生成.解析的库.最近工作需求去研究了一下,主要是研究怎么扫描二维码(QRCode).网上教程也不少,但大多看了不明所以,甚至看了半天都不知道解码到底从哪 ...

  3. codevs 1200 同余方程 (Extend_Eulid)

    /* 扩展欧几里得 ax%b==1 -> ax-by==1 求不定方程的一组解 使x为最小正整数解 */ #include<iostream> #include<cstdio& ...

  4. dedecms网站文章标题与简标题的调用问题

    使用dedecms调用标签的时候,既然有,咱们就合理利用,如果没有,咱也可以自己去添加.以下介绍dedecms网站文章标题调用的一些技巧,希望大家能够合理运用. dedecms网站文章标题与简标题的调 ...

  5. Swift - 23 - 选择结构

    //: Playground - noun: a place where people can play import UIKit var rating = "A" // if - ...

  6. phpstudy apache 刚启动便停止

    1.添加站点 2.重启服务 3.遇见问题 apache 刚启动,1秒钟中后就停止 4.解决问题 发现是自己添加的网站中包含中文路径的问题,建议不要在自己的网站目录下包含中文.

  7. html5 高清屏幕图片处理

    1. srcset 语法:在元素上添加srcset属性.srcset的值是一个用逗号分隔的列表.列表中的每个项包含一张图片的路径并且按倍数(例如,1x,2x,3x...)提供多张分辨率的图片 参考:h ...

  8. plist解析, 简易实现.

    源码 class Xml { public: typedef std::pair<std::wstring, std::wstring> NodeT; static std::vector ...

  9. 【USACO 2.4.5】分数化小数

    [描述] 写一个程序,输入一个形如N/D的分数(N是分子,D是分母),输出它的小数形式. 如果小数有循环节的话,把循环节放在一对圆括号中. 例如, 1/3 =0.33333333 写成0.(3), 4 ...

  10. javascript获取地址栏参数/值

    //URL: http://www.example.com/?var1=val1&var2=val2=val3&test=3&test=43&aaa=#2 //wind ...