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

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. Eclipse常用快捷键 及 不格式化注释

    eclipse不格式化注释 - [自写] 2007-08-15   刚才在Eclipse3.2上写东西,我写好的注释,整整齐齐的,我一格式化代码,就变得七七八八的了.   试着在Perferences ...

  2. WCF(四) 绑定

    绑定 是一个制定好的通道栈,包含了协议通道,传输通道和编码器.从功能上来看,一个绑定集成了通信模式.可靠性.安全性.事务传播和互操作性 绑定方式分两种:代码中和配置文件中绑定 1: 2: 3.配置ap ...

  3. windows dos 常用命令行

    有关某个命令的详细信息,请键入 HELP 命令名 dir (directory) :列出当前目录下的文件以及文件夹 md (make directory): 创建目录 rd (remove direc ...

  4. SQLErrorCodes loaded: [DB2, Derby, H2, HSQL, Informix, MS-SQL, MySQL, Oracle, PostgreSQL, Sybase, Hana]

    Loading XML bean definitions from class path resource [org/springframework/jdbc/support/sql-error-co ...

  5. php中 const 与define()的区别 ,选择

    来自: http://stackoverflow.com/questions/2447791/define-vs-const 相同点: 两者都可以定义常量 const FOO = 'BAR'; def ...

  6. In ZeroDB, the client is responsible for the database logic. Data encryption, decryption, and compression also happen client side. Therefore, the server never has any knowledge about the data, its str

    zerodb/index.rst at master · zerodb/zerodb https://github.com/zerodb/zerodb/blob/master/docs/source/ ...

  7. socket套接字TCP协议传输-案例测试

    术语: 套接字接口:socket,是一个IP地址和一个端口号的组合,套接字可以唯一标识整个Internet中的一个网络进程. TCP连接:一对套接字接口(一个用于接收,一个用于发送)可定义面向连接的协 ...

  8. git-【八】多人协作

    当你从远程库克隆时候,实际上Git自动把本地的master分支和远程的master分支对应起来了,并且远程库的默认名称是origin. 要查看远程库的信息 使用 git remote 要查看远程库的详 ...

  9. 博客迁移至新平台ixirong.com

    很久没有在博客园上写文章了,一是时间有些忙,更重要的是自己还是没有抽出时间来坚持写下去,由于15年后自己的一些打算,在前一段时间的时候,建立了自己的个人博客站点,http://www.ixirong. ...

  10. C++ vector错误(1)

    在用C++的vector的时候,要保证访问的下标不能超过vector的size.否则出现msvcp60.dll 访问禁止.