111 Minimum Depth of Binary Tree 二叉树的最小深度
给定一个二叉树,找出其最小深度。
最小深度是从根节点到最近叶节点的最短路径的节点数量。
详见:https://leetcode.com/problems/minimum-depth-of-binary-tree/description/
Java实现:
递归实现:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
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;
}
}
非递归实现:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public int minDepth(TreeNode root) {
if(root==null){
return 0;
}
LinkedList<TreeNode> que=new LinkedList<TreeNode>();
que.offer(root);
int node=1;
int level=1;
while(!que.isEmpty()){
root=que.poll();
--node;
if(root.left==null&&root.right==null){
return level;
}
if(root.left!=null){
que.offer(root.left);
}
if(root.right!=null){
que.offer(root.right);
}
if(node==0){
node=que.size();
++level;
}
}
return level;
}
}
111 Minimum Depth of Binary Tree 二叉树的最小深度的更多相关文章
- [LeetCode] 111. Minimum Depth of Binary Tree ☆(二叉树的最小深度)
[Leetcode] Maximum and Minimum Depth of Binary Tree 二叉树的最小最大深度 (最小有3种解法) 描述 解析 递归深度优先搜索 当求最大深度时,我们只要 ...
- [LeetCode] 111. 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】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- [LeetCode] 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] The 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 111 Minimum Depth of Binary Tree 二叉树
找出最短的从叶子到根的路径长 可以回忆Maximum Depth of Binary Tree的写法,只不过在!root,我把它改成了10000000,还有max函数改成了min函数,最后的值如果是1 ...
- LeetCode:111_Minimum Depth of Binary Tree | 二叉树的最小深度 | Easy
要求:此题正好和Maximum Depth of Binary Tree一题是相反的,即寻找二叉树的最小的深度值:从根节点到最近的叶子节点的距离. 结题思路:和找最大距离不同之处在于:找最小距离要注意 ...
- 【LeetCode】111. Minimum Depth of Binary Tree (2 solutions)
Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth is the n ...
- (二叉树 BFS DFS) leetcode 111. Minimum Depth of Binary Tree
Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...
随机推荐
- SCAU RP Test —— 因式分解与组合
D RP Test Time Limit:1000MS Memory Limit:65535K 题型: 编程题 语言: 无限制 描述 LRC是SCAU_ACM校队的主席,职业生涯为校队作过很多 ...
- Nginx的Location正则表达式
location的作用 location指令的作用是根据用户请求的URI来执行不同的应用,也就是根据用户请求的网站URL进行匹配,匹配成功即进行相关的操作. location的语法 已=开头表示精确匹 ...
- 基于Jquery的XML解析器,返回定制的HTML
依据HTML模板返回解析的XML 依赖jQuery 1.41. [代码]基于Jquery的xml解析器并返回定制的HTML /** * jQuery插件 * Author: pureco ...
- 在Angular.js中的H5页面调用Web api时跨域问题处理
/// <summary> /// 被请求时 /// 在Angular.js中的H5页面调用Web api时跨域问题处理 /// </summary> /// <para ...
- Linux 修改所属组与所属人
1.chown work /test/* 修改test文件夹下所有文件的所属人(owner)为work 2.chgrp work /test/* 修改test文件夹下所有文件的所属组(group)为w ...
- LA-3942(trie树+dp)
题意: 给出一个由多个不同单词组成的字典,和一个长字符串,把这个字符串分解成若干个单词的连接,问有多少种方法; 思路: dp[i]表示s[i,L]的方案数,d[i]=∑d[j];s[i,j-1]是一个 ...
- 插入排序(InsertionSort)
位置p上的元素存储于tmp(第一趟p通常取1),而(位置p之前)所有更大的元素都向右移动一个位置. 然后tmp被放在正确的位置上. 代码: public class InsertionSort { p ...
- 关闭页面,window.onunload事件未执行的原因
1.问题描述: JS中定义widow.onunload= function(),页面关闭时,logout()函数未执行. window.onunload = function() { logout() ...
- Objective-C 中nil/Nil/NULL/NSNull
转自:http://nshipster.cn/nil/ 理解"不存在"的概念不仅仅是一个哲学的问题,也是一个实际的问题.我们是有形宇宙的居民,而原因在于逻辑宇宙的存在不确定性.作为 ...
- kafka数据可靠性深度解读【转】
1 概述 Kakfa起初是由LinkedIn公司开发的一个分布式的消息系统,后成为Apache的一部分,它使用Scala编写,以可水平扩展和高吞吐率而被广泛使用.目前越来越多的开源分布式处理系统如Cl ...