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 shortest path from the root node down to the nearest leaf node.
题解:
递归解法急速判断左右两边子树哪个depth最小,要注意如果有个节点只有一边孩子时,不能返回0,要返回另外一半边的depth。
递归解法:
public int minDepth(TreeNode root) {
if(root == null)
return 0;
int minleft = minDepth(root.left);
int minright = minDepth(root.right);
if(minleft==0 || minright==0)
return minleft>=minright?minleft+1:minright+1;
return Math.min(minleft,minright)+1;
}
非递归解法:
1 public int minDepth(TreeNode root) {
2 if(root == null)
3 return 0;
4
5 int depth = 1;//The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.
6 LinkedList<TreeNode> queue = new LinkedList<TreeNode>();
7 queue.add(root);
8 int curnum = 1;
9 int nextnum = 0;
while(!queue.isEmpty()){
TreeNode cur = queue.poll();
curnum--;
if(cur.left == null && cur.right == null)
return depth;
if(cur.left != null){
queue.add(cur.left);
nextnum++;
}
if(cur.right != null){
queue.add(cur.right);
nextnum++;
}
if(curnum == 0){
curnum = nextnum;
nextnum = 0;
depth++;
}
}
return depth;
}
Minimum Depth of Binary Tree leetcode java的更多相关文章
- 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 ...
- 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 ...
- Maximum Depth of Binary Tree leetcode java
题目: Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the ...
- LeetCode算法题-Minimum Depth of Binary Tree(Java实现)
这是悦乐书的第168次更新,第170篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第27题(顺位题号是111).给定二叉树,找到它的最小深度.最小深度是沿从根节点到最近的 ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- [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] 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 ...
随机推荐
- 洛谷P1527 [国家集训队] 矩阵乘法 [整体二分,二维树状数组]
题目传送门 矩阵乘法 题目描述 给你一个N*N的矩阵,不用算矩阵乘法,但是每次询问一个子矩形的第K小数. 输入输出格式 输入格式: 第一行两个数N,Q,表示矩阵大小和询问组数: 接下来N行N列一共N* ...
- linux 下安装jdk环境安装
一.创建jdk安装目录mkdir /usr/local/java 二.将jdk解压到安装目录中,直接到java目录中,如果不是处理下不要有子目录 tar -zxvf jdk-8u91-linux-x6 ...
- 洛谷——P2756 飞行员配对方案问题
P2756 飞行员配对方案问题 题目背景 第二次世界大战时期.. 题目描述 英国皇家空军从沦陷国征募了大量外籍飞行员.由皇家空军派出的每一架飞机都需要配备在航行技能和语言上能互相配合的2 名飞行员,其 ...
- JDK源码分析(四)——LinkedHashMap
目录 LinkedHashMap概述 内部字段及构造方法 存储元素 取出元素 删除元素 迭代器 利用LinkedHashMap简单实现LRU算法 总结 LinkedHashMap概述 JDK对Li ...
- golang make()的第三个参数
golang分配内存有一个make函数,该函数第一个参数是类型,第二个参数是分配的空间,第三个参数是预留分配空间,前两个参数都很好理解, 对于第三个参数,例如a:=make([]int, 5, 10) ...
- XShell通过中转服务器直接连接目标服务器
最近由于公司生产环境的变化,使得我们不能使用自己的机器连接到生产环境去,而是要通过跳板机中转才可以连接.于是今天尝试使用 XShell 通过跳板机直接转接到生产环境. 一.使用代理方式 首先填写连接信 ...
- QQ怎么 发送 已经录好的视频
韩梦飞沙 韩亚飞 313134555@qq.com yue31313 han_meng_fei_sha QQ发送 已经录好的视频 直接放过去,对方是需要下载的. 只有通过QQ录制的,才是直接就 ...
- Qt Quick快速入门之qml布局
Qml里面布局主要有两种,锚点布局.Grid布局. 锚点布局使用anchors附件属性将一个元素的边定位到另一个元素的边,从而确定元素的位置和大小.下面是示例 import QtQuick 2.3 i ...
- 【HDU 3662】3D Convex Hull
http://acm.hdu.edu.cn/showproblem.php?pid=3662 求给定空间中的点的三维凸包上有多少个面. 用增量法,不断加入点,把新加的点能看到的面都删掉,不能看到的面与 ...
- BZOJ 2302: [HAOI2011]Problem c [DP 组合计数]
2302: [HAOI2011]Problem c Time Limit: 30 Sec Memory Limit: 256 MBSubmit: 648 Solved: 355[Submit][S ...