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 ...
随机推荐
-
【最短路】ACdream 1198 - Transformers&#39; Mission
Problem Description A group of transformers whose leader is Optimus Prime(擎天柱) were assigned a missi ...
-
深入理解JavaWeb技术内幕(一)
最近在看许令波的<深入理解JavaWeb技术内幕>.整理了一些笔记.想做一个系列,这篇是系列的第一篇,讲Web请求. B/S架构 最常见的架构方式. 优点: 1.客户端使用统一(此处的统一 ...
-
关于Unix时间戳
如何在不同编程语言中获取现在的Unix时间戳(Unix timestamp)? Java time JavaScript Math.round(new Date().getTime()/1000)ge ...
-
java中用正則表達式推断中文字符串中是否含有英文或者数字
public static boolean includingNUM(String str)throws Exception{ Pattern p = Pattern.compile(" ...
-
拼接SQL执行语句时,对单引号的处理
例: declare @SQL nvarchar(1000); declare @str nvarchar(100); set @str='Joe''s NB'; // 打印出来的应该是这样:Joe' ...
-
Android系统裁剪:手把手教你如何进行系统裁剪
前言:android系统裁剪优化一直是各个厂商定制产品的关键步骤,包括浅层次的去除不必要的apk(android apk裁剪定制 )和深层次的裁剪整个编译系统和框架层. android作为开源系统 ...
-
adv生成控制器手腕位置倾斜原因以及解决方案
系统默认问题导致手腕倾斜详情描述: 手腕部分默认生成轴向是冲向模板下一层级第一个物体 简单说就是 FK轴向冲向模板中指方向 如图 默认模板没问题是因为 默认模板没有改动情况下系统中指与手腕在一条直 ...
-
初始js闭包&;事件的冒泡和捕获&;EVENT对象
一.初识闭包 function a(){ var n = 0; this.inc = function () { n++; console.log(n); }; } var ...
-
三种进程和线程数据共享模块方法Queue》Pipe》manager
>>>>线程中的queue import threading import queue def f(qq): print("in child",qq.qsi ...
-
codeforces 788A Functions again
…… 原题: Something happened in Uzhlyandia again... There are riots on the streets... Famous Uzhlyandia ...