Minimum Depth of Binary Tree,求树的最小深度
算法分析:递归和非递归两种方法。
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,求树的最小深度的更多相关文章
- 【easy】111. Minimum Depth of Binary Tree求二叉树的最小深度
求二叉树的最小深度: /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; ...
- 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 ...
- LeetCode Maximum Depth of Binary Tree (求树的深度)
题意:给一棵二叉树,求其深度. 思路:递归比较简洁,先求左子树深度,再求右子树深度,比较其结果,返回:max_one+1. /** * Definition for a binary tree nod ...
- [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 ...
- 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 ...
- LeetCode OJ Minimum Depth of Binary Tree 递归求解
题目URL:https://leetcode.com/problems/minimum-depth-of-binary-tree/ 111. Minimum Depth of Binary T ...
- 【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 ...
- LeetCode My Solution: Minimum Depth of Binary Tree
Minimum Depth of Binary Tree Total Accepted: 24760 Total Submissions: 83665My Submissions Given a bi ...
- [LeetCode] 111. Minimum Depth of Binary Tree ☆(二叉树的最小深度)
[Leetcode] Maximum and Minimum Depth of Binary Tree 二叉树的最小最大深度 (最小有3种解法) 描述 解析 递归深度优先搜索 当求最大深度时,我们只要 ...
- 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 ...
随机推荐
- Mac OS X下配置远程Linux 服务器SSH密钥认证自动登录
1. 在本地机器创建公钥 打开万能的终端,执行如下命令,无视一切输出,一路欢快地回车即可. ssh-keygen -t rsa -C 'your email@domain.com' -t 指定密钥类型 ...
- 【BZOJ2957】楼房重建 分块
[BZOJ2957]楼房重建 Description 小A的楼房外有一大片施工工地,工地上有N栋待建的楼房.每天,这片工地上的房子拆了又建.建了又拆.他经常无聊地看着窗外发呆,数自己能够看到多少栋房子 ...
- DES加密解密 Java中运用
DES全称Data Encryption Standard,是一种使用密匙加密的块算法.现在认为是一种不安全的加密算法,因为现在已经有用穷举法攻破DES密码的报道了.尽管如此,该加密算法还是运用非常普 ...
- javascript飞机大战-----003创建英雄机
/* 英雄机:因为英雄机只有一辆所以不需要用构造函数 */ var Hero = { //初始图片 self:null, //初始left left:0, //初始top top:0, //生命值 l ...
- 自定义Realm解析
自定义Realm解析---------------------------------------> /* * Copyright 2005-2013 shopxx.net. All right ...
- Oracle Schema Objects——Sequences(伪列:nextval,currval)
Oracle Schema Objects 序列的作用 许多的数据库之中都会为用户提供一种自动增长列的操作,例如:在微软的Access数据库之中就提供了一种自动编号的增长列(ID列).在oracle数 ...
- NGINX:sticky模块实现基于cookie的负载均衡
Sticky模块 简述: 之前公司部署了一套网站及时发布系统,架构如下图所示:Nginx做前端代理,发布系统用tomcat运行,一台共享存储,一台数据库服务器:由于网站及时发布系统涉及到了用户登录操作 ...
- P2750 贰五语言Two Five USACO5.5 记忆化搜索
正解:记搜+逼近 解题报告: 神仙题预警,,, 我真滴觉得还是挺难的了,,, 大概说下思路趴QAQ 首先我们要知道逼近法是什么! 逼近法,有点像二分的思路,以这题为例举个eg 假如它给了个数字k.我们 ...
- Nginx rewrite 中break与last指令的区别
location /break/ { rewrite ^/break/(.*) /test/$1 break; return 402; } location /last/ { rewrite ^/la ...
- oracle基于3种方法的大数据量插入更新
过程插入更新的3种方法: a.逐条检查插入或更新,同时执行插入或更新 b.逐条merge into(逐条是为了记录过程日志与错误信息) c.基于关联数组的检查插入.更新,通过forall批量sql执行 ...