算法分析:递归和非递归两种方法。

public class MinimumDepthofBinaryTree {

	//递归,树的最小深度,就是它左右子树的最小深度的最小值+1
public int minDepth(TreeNode root) {
if(root == null)
{
return 0;
}
int lmin = minDepth(root.left);
int rmin = minDepth(root.right);
if(lmin == 0 && rmin == 0)
{
return 1;
}
if(lmin == 0)//左子树为空,右子树不为空,则最小深度为右子子树的最小深度+1
{
return rmin + 1;
}
if(rmin == 0)
{
return lmin + 1;
}
return Math.min(lmin, rmin)+1;
} //非递归,按层遍历,找到第一个叶子结点
public int minDepth2(TreeNode root) {
if(root == null)
{
return 0;
}
ArrayList<TreeNode> list = new ArrayList<>();
int count = 1;//初始值为1
list.add(root);
while(!list.isEmpty())
{
ArrayList<TreeNode> temp = new ArrayList<>();
for (TreeNode node : list) {
if(node.left == null && node.right == null)//当节点左右子树都为空时,该节点为第一个叶子节点,该节点的深度即为树的最小深度
{
return count;
}
if(node.left != null)
{
temp.add(node.left);
}
if(node.right != null)
{
temp.add(node.right);
}
}
count ++;//按层遍历,每循环一次,就是一层,层数加1
list = temp;
}
return count;
}
}

  

Minimum Depth of Binary Tree,求树的最小深度的更多相关文章

  1. 【easy】111. Minimum Depth of Binary Tree求二叉树的最小深度

    求二叉树的最小深度: /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; ...

  2. LeetCode OJ: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 Maximum Depth of Binary Tree (求树的深度)

    题意:给一棵二叉树,求其深度. 思路:递归比较简洁,先求左子树深度,再求右子树深度,比较其结果,返回:max_one+1. /** * Definition for a binary tree nod ...

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

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

  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练习题】Minimum 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 My Solution: Minimum Depth of Binary Tree

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

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

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

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

随机推荐

  1. JQueryUI之Autocomplete

    JQueryUI之Autocomplete JQuery UI 是以 JQuery 为基础的开源 JavaScript 网页用户界面代码库.包含底层用户交互.动画.特效和可更换主题的可视控件,这些控件 ...

  2. CodeForces 17D Notepad(同余定理)

    D. Notepad time limit per test 2 seconds memory limit per test 64 megabytes input standard input out ...

  3. BeanFactory 使用控制反转 (IOC) 模式将应用程序的配置和依赖性规范与实际的应用程序代码分开。面向切面 将声明性事务管理集成到应用程序中

    Spring 系列: Spring 框架简介 https://www.ibm.com/developerworks/cn/java/wa-spring1/ Spring 框架简介 Spring AOP ...

  4. rbac - 界面、权限

    一.模板继承 知识点: users.html / roles.html 继承自 base.html 滑动时,固定 position: fixed;top:60px;bottom:0;left:0;wi ...

  5. 转!!spring @component 详解 默认初始化bean的名字 VNumberTask类 就是 VNumberTask

    参考链接:信息来源 今天碰到一个问题,写了一个@Service的bean,类名大致为:CUser xml配置: <context:component-scan base-package=&quo ...

  6. 设置UCHome注册登陆退出的跳转页自定义

    UCHome 默认注册.登录成功后跳转到 http://www.xxx.com/home/space.php?do=home 退出后会跳转到站点的首页,即 http://www.xxx.com/hom ...

  7. 数据展现-百度js绘图

    echarts:酷炫的绘图效果 http://echarts.baidu.com/examples/#chart-type-calendar

  8. CSV文件读取类

    最近项目中,经常需要读取Csv文件.基本步骤是: (1)按行读取 (2)然后将一行数据按逗号,分割为字符串数组 (3)将各列字符串转换成相应类型的数据 ,如int double类型 写了一个简单的Cs ...

  9. Console 窗口

    Console窗口 记住,即是在GUI程序中你也可以拥有一个Console窗口.----这意味着你可以再GUI程序中使用printf.puts. Console窗口由系统的驱动设备程序负责,即是你的程 ...

  10. 1 - bootstrap基本模板

    bootstrap 3.x 下载地址:http://v3.bootcss.com/ 基本模板如下: <!DOCTYPE html> <html lang="zh-cn&qu ...