[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 shortest path from the root node down to the nearest leaf node.
给一个二叉树,找出它的最小深度。最小深度是从根节点向下到最近的叶节点的最短路径,就是最短路径的节点个数。
解法1:DFS
解法2: BFS
Java: DFS, Time Complexity: O(n), Space Complexity: O(n)
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public int minDepth(TreeNode root) {
if (root == null) {
return 0;
}
if (root.left == null) {
return 1 + minDepth(root.right);
} else if (root.right == null) {
return 1 + minDepth(root.left);
} else {
return 1 + Math.min(minDepth(root.left), minDepth(root.right));
}
}
}
Java: BFS
public class Solution {
public int minDepth(TreeNode root) {
if (root == null) {
return 0;
}
Queue<TreeNode> q = new LinkedList<>();
q.offer(root);
int curLevel = 1, nextLevel = 0;
int depth = 1; while (!q.isEmpty()) {
TreeNode node = q.poll();
curLevel--;
if (node.left == null && node.right == null) {
return depth;
}
if (node.left != null) {
q.offer(node.left);
nextLevel++;
}
if (node.right != null) {
q.offer(node.right);
nextLevel++;
}
if (curLevel == 0) {
curLevel = nextLevel;
nextLevel = 0;
depth++;
}
}
return depth;
}
}
Python:
class Solution(object):
def minDepth(root):
if root is None:
return 0 # Base Case : Leaf node.This acoounts for height = 1
if root.left is None and root.right is None:
return 1 if root.left is None:
return minDepth(root.right) + 1 if root.right is None:
return minDepth(root.left) + 1 return min(minDepth(root.left), minDepth(root.right)) + 1
Python:
class Solution:
# @param root, a tree node
# @return an integer
def minDepth(self, root):
if root is None:
return 0 if root.left and root.right:
return min(self.minDepth(root.left), self.minDepth(root.right)) + 1
else:
return max(self.minDepth(root.left), self.minDepth(root.right)) + 1
C++:
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int minDepth(TreeNode *root) {
if (root == NULL) return 0;
if (root->left == NULL && root->right == NULL) return 1; if (root->left == NULL) return minDepth(root->right) + 1;
else if (root->right == NULL) return minDepth(root->left) + 1;
else return 1 + min(minDepth(root->left), minDepth(root->right));
} };
类似题目:
[LeetCode] 104. Maximum Depth of Binary Tree 二叉树的最大深度
All LeetCode Questions List 题目汇总
[LeetCode] 111. Minimum Depth of Binary Tree 二叉树的最小深度的更多相关文章
- [LeetCode] 111. Minimum Depth of Binary Tree ☆(二叉树的最小深度)
[Leetcode] Maximum and Minimum Depth of Binary Tree 二叉树的最小最大深度 (最小有3种解法) 描述 解析 递归深度优先搜索 当求最大深度时,我们只要 ...
- 【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] 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 ...
- 111 Minimum Depth of Binary Tree 二叉树的最小深度
给定一个二叉树,找出其最小深度.最小深度是从根节点到最近叶节点的最短路径的节点数量.详见:https://leetcode.com/problems/minimum-depth-of-binary-t ...
- Leetcode 111 Minimum Depth of Binary Tree 二叉树
找出最短的从叶子到根的路径长 可以回忆Maximum Depth of Binary Tree的写法,只不过在!root,我把它改成了10000000,还有max函数改成了min函数,最后的值如果是1 ...
- [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:111_Minimum Depth of Binary Tree | 二叉树的最小深度 | Easy
要求:此题正好和Maximum Depth of Binary Tree一题是相反的,即寻找二叉树的最小的深度值:从根节点到最近的叶子节点的距离. 结题思路:和找最大距离不同之处在于:找最小距离要注意 ...
- (二叉树 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 ...
- 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 ...
随机推荐
- Linux代理服务器使用
1. 介绍 代理(即网络代理)是一种特殊的网络服务, 允许一个网络终端(客户端)通过这个服务与另一个终端(服务器)进行非直接连接,从而提供服务. 其中, 提供代理的网络终端称为代理服务器(Proxy ...
- myshell
要求 利用fork,exec,wait编写一个具有执行命令功能的shell
- 解决页面初始化vue加载代码问题
<style type="text/css"> /* 解决页面初始化vue加载代码问题 */ [v-cloak] { display: none; } </sty ...
- How Open Source Became The Default Business Model For Software
https://www.forbes.com/sites/forbestechcouncil/2018/07/16/how-open-source-became-the-default-busines ...
- hdu3625
hdu3625 题意: 酒店发生一起谋杀案.作为镇上最好的侦探,您应该立即检查酒店的所有N个房间.但是,房间的所有门都是锁着的,钥匙刚锁在房间里,真是个陷阱!您知道每个房间里只有一把钥匙,并且所有可能 ...
- python接口自动化—封装获取常量的类
背景: 一.执行case的过程: 首先需要,我们能够通过excel获取单元格的内容.获取内容时,首先需要知道获取的数据是哪一行的,这行数据中需要拿那些参数,比如case 名称.请求url.请求方式.h ...
- 开源项目 07 AutoMapper
using AutoMapper; using Newtonsoft.Json; using System; using System.Collections.Generic; using Syste ...
- C# VS 调试 动态加载的 DLL
原文:https://www.cnblogs.com/DasonKwok/p/10510218.html 在这篇文章的底部,有提供示例的Demo,可以参考一下哦,拿来直接就可以运行. 说明: 编译类库 ...
- 1-开发共享版APP(源码介绍)-BUG修复
这一系列文章将介绍APP的源码,这一节作为所有BUG问题修复! https://www.cnblogs.com/yangfengwu/category/1512162.html //开发共享版A ...
- [CSP-S 2019]括号树
[CSP-S 2019]括号树 源代码: #include<cstdio> #include<cctype> #include<vector> inline int ...