N1-1 - 树 - 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.
特殊输入:
只有两个节点时,树的深度是2。但是如果只考虑左子树或右子树为空。结果会返回1.
两个错误的代码:(不应该以子树空判断结束)
实现1:该代码是错误的
class Solution {
public:
int run(TreeNode *root) {
if(root==nullptr)
return 0; queue<TreeNode *> currQueue;
currQueue.push(root);
int count = 0;
while(!currQueue.empty()){
count++;
for(int i=0;i<currQueue.size();i++){ //遍历一层
TreeNode * pNode = currQueue.front();
currQueue.pop();
if(pNode->left)
currQueue.push(pNode->left);
else
return count;
if(pNode->right)
currQueue.push(pNode->right);
else
return count; }
}
return count;
}
}
//实现2: 该代码是错误的
int getMinDepth(TreeNode *root){
if(root==nullptr)
return 0;
return min(getMinDepth(root->left),getMinDepth(root->right))+1;
}
解题思路:
1)一直访问到叶子节点,层次遍历遇到的最早的叶子节点,当前层就是树的最小深度。(使用队列,非递归)
1
/ \
2 3
/
4
错误的方法是:当子树为空,返回当前层(上面的例子可以)
但是下面的例子会返回1。
1
/
2
class Solution {
public:
int run(TreeNode *root) {
if(root==nullptr)
return 0;
queue<TreeNode *> currQueue;
currQueue.push(root);
int count = 0;
while(!currQueue.empty()){
count++;
int size = currQueue.size();
for(int i=0;i<size;i++){ //遍历一层
TreeNode * pNode = currQueue.front();
currQueue.pop();
if(pNode->left==nullptr && pNode->right==nullptr) //遇到的第一个叶子节点,返回当前层
return count;
if(pNode->left)
currQueue.push(pNode->left);
if(pNode->right)
currQueue.push(pNode->right);
}
}
return count;
}
};
2)思路:递归
//实现1
class Solution {
public:
int run(TreeNode *root)
{
if(root == nullptr) return 0;
if(root->left == nullptr) // 若左子树为空,则返回右子树的最小深度+1
{
return run(root->right)+1;
}
if(root->right == nullptr) // 若右子树为空,则返回左子树的最小深度+1
{
return run(root->left)+1;
}
// 左右子树都不为空时,取较小值
int leftDepth = run(root->left);
int rightDepth = run(root->right);
return (leftDepth<rightDepth)?(leftDepth+1):(rightDepth+1);
}
};
//实现2
class Solution {
public:
int run(TreeNode *root) {
if(!root)
return 0;
int l = run(root->left);
int r = run(root->right);
if(l==0 || r==0)
return 1+l+r;
return 1+min(l,r);
}
};
3)深度遍历要遍历所有节点,因此选广度优先遍历更好。不再给出深度遍历的代码
总结:
二叉树操作主要还是利用尾递归或者循环遍历这两种思路,进而涉及DFS(主要利用递归或者栈实现)或者BFS(主要利用队列实现)。
N1-1 - 树 - Minimum Depth of Binary Tree的更多相关文章
- [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: 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 OJ Minimum Depth of Binary Tree 递归求解
题目URL:https://leetcode.com/problems/minimum-depth-of-binary-tree/ 111. Minimum Depth of Binary T ...
- 33. Minimum Depth of Binary Tree && Balanced Binary Tree && Maximum Depth of Binary Tree
Minimum Depth of Binary Tree OJ: https://oj.leetcode.com/problems/minimum-depth-of-binary-tree/ Give ...
- 【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】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- 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种解法) 描述 解析 递归深度优先搜索 当求最大深度时,我们只要 ...
随机推荐
- 安装GCC for Red Hat Enterprise Linux Server release 6(64位)
http://www.cnblogs.com/emanlee/archive/2012/08/11/2633895.html
- 草草搞了个SERVLET的注册登陆功能
按书来的,学习阶段,一切都好新奇..至少对比于DJANGO,好像复杂点点,但HTTP的东东,是相通的哈. package cc.openhome.controller; import java.io. ...
- 洛谷——P1002 过河卒||codevs——T1010 过河卒
https://www.luogu.org/problem/show?pid=1002#sub||http://codevs.cn/problem/1010/ 题目描述 棋盘上A点有一个过河卒,需要走 ...
- 洛谷 P2896 [USACO08FEB]一起吃饭Eating Together
P2896 [USACO08FEB]一起吃饭Eating Together 题目描述 The cows are so very silly about their dinner partners. T ...
- PHP独立操作符
& 与 ^ 位逻辑异或 $ # ! 逻辑或 ~ 按位取反
- HTTP缓存和CDN缓存
一 http缓存 1.1缓存的分类: http中具有缓存功能的是:1.浏览器缓存. 2.缓存代理服务器. 1.2 什么是缓存: http缓存的是指:当Web请求抵达缓存时, 如果本地有“已缓存的”副 ...
- EM算法——有隐含变量时,极大似然用梯度法搞不定只好来猜隐含变量期望值求max值了
摘自:https://www.zhihu.com/question/27976634 简单说一下为什么要用EM算法 现在一个班里有50个男生,50个女生,且男生站左,女生站右.我们假定男生的身高服从正 ...
- 【POJ 1703】 Find them,Catch them
[题目链接] http://poj.org/problem?id=1703 [算法] 并查集 + 拆点 [代码] #include <algorithm> #include <bit ...
- 升鲜宝V2.0_杭州生鲜配送行业,升鲜宝供应链手机分拣系统使用说明_升鲜宝生鲜供应链管理系统_15382353715_余东升
1. 系统概述 升鲜宝供应链手机分拣系统(Android版)是升鲜宝供应链管理系统的配套分拣系统,本系统适用于农副产品生鲜配送供应链企业分拣过程,在线上数据管理和线下业务管理两大方面提供解决方案,该系 ...
- EditPlus代码自动完成的设置
EditPlus代码自动完成的设置保存在 *.acp 文件中,可以在“工具”->“首选项”->“文件”->“文件类型及语法”中(如下图) 其中“语法文件”保存着进行语法高亮的关键词, ...