当前位置: 首页 > news >正文

百汇游戏网站开发商镇江网站定制

百汇游戏网站开发商,镇江网站定制,wordpress+下载受限,vpswindows俄罗斯力扣题目链接(opens new window) 给定一个二叉树,找出其最小深度。 最小深度是从根节点到最近叶子节点的最短路径上的节点数量。 说明: 叶子节点是指没有子节点的节点。 示例: 给定二叉树 [3,9,20,null,null,15,7], 返回它的最小深度 2 思路 看完了这篇104.二…

力扣题目链接(opens new window)

给定一个二叉树,找出其最小深度。

最小深度是从根节点到最近叶子节点的最短路径上的节点数量。

说明: 叶子节点是指没有子节点的节点。

示例:

给定二叉树 [3,9,20,null,null,15,7],

111.二叉树的最小深度1

返回它的最小深度 2

思路

看完了这篇104.二叉树的最大深度 (opens new window),再来看看如何求最小深度。

直觉上好像和求最大深度差不多,其实还是差不少的。

本题依然是前序遍历和后序遍历都可以,前序求的是深度,后序求的是高度。

  • 二叉树节点的深度:指从根节点到该节点的最长简单路径边的条数或者节点数(取决于深度从0开始还是从1开始)
  • 二叉树节点的高度:指从该节点到叶子节点的最长简单路径边的条数后者节点数(取决于高度从0开始还是从1开始)

那么使用后序遍历,其实求的是根节点到叶子节点的最小距离,就是求高度的过程,不过这个最小距离 也同样是最小深度。

以下讲解中遍历顺序上依然采用后序遍历(因为要比较递归返回之后的结果,本文我也给出前序遍历的写法)。

本题还有一个误区,在处理节点的过程中,最大深度很容易理解,最小深度就不那么好理解,如图:

111.二叉树的最小深度

这就重新审题了,题目中说的是:最小深度是从根节点到最近叶子节点的最短路径上的节点数量。,注意是叶子节点

什么是叶子节点,左右孩子都为空的节点才是叶子节点!

#递归法

来来来,一起递归三部曲:

  1. 确定递归函数的参数和返回值

参数为要传入的二叉树根节点,返回的是int类型的深度。

代码如下:

int getDepth(TreeNode* node)
  1. 确定终止条件

终止条件也是遇到空节点返回0,表示当前节点的高度为0。

代码如下:

if (node == NULL) return 0;

  1. 确定单层递归的逻辑

这块和求最大深度可就不一样了,一些同学可能会写如下代码:

int leftDepth = getDepth(node->left);
int rightDepth = getDepth(node->right);
int result = 1 + min(leftDepth, rightDepth);
return result;

这个代码就犯了此图中的误区:

111.二叉树的最小深度

如果这么求的话,没有左孩子的分支会算为最短深度。

所以,如果左子树为空,右子树不为空,说明最小深度是 1 + 右子树的深度。

反之,右子树为空,左子树不为空,最小深度是 1 + 左子树的深度。 最后如果左右子树都不为空,返回左右子树深度最小值 + 1 。

代码如下:

int leftDepth = getDepth(node->left);           // 左
int rightDepth = getDepth(node->right);         // 右// 中
// 当一个左子树为空,右不为空,这时并不是最低点
if (node->left == NULL && node->right != NULL) { return 1 + rightDepth;
}   
// 当一个右子树为空,左不为空,这时并不是最低点
if (node->left != NULL && node->right == NULL) { return 1 + leftDepth;
}
int result = 1 + min(leftDepth, rightDepth);
return result;

遍历的顺序为后序(左右中),可以看出:求二叉树的最小深度和求二叉树的最大深度的差别主要在于处理左右孩子不为空的逻辑。

整体递归代码如下:

class Solution {
public:int getDepth(TreeNode* node) {if (node == NULL) return 0;int leftDepth = getDepth(node->left);           // 左int rightDepth = getDepth(node->right);         // 右// 中// 当一个左子树为空,右不为空,这时并不是最低点if (node->left == NULL && node->right != NULL) { return 1 + rightDepth;}   // 当一个右子树为空,左不为空,这时并不是最低点if (node->left != NULL && node->right == NULL) { return 1 + leftDepth;}int result = 1 + min(leftDepth, rightDepth);return result;}int minDepth(TreeNode* root) {return getDepth(root);}
};

精简之后代码如下:

class Solution {
public:int minDepth(TreeNode* root) {if (root == NULL) return 0;if (root->left == NULL && root->right != NULL) {return 1 + minDepth(root->right);}if (root->left != NULL && root->right == NULL) {return 1 + minDepth(root->left);}return 1 + min(minDepth(root->left), minDepth(root->right));}
};

精简之后的代码根本看不出是哪种遍历方式,所以依然还要强调一波:如果对二叉树的操作还不熟练,尽量不要直接照着精简代码来学。

前序遍历的方式:

class Solution {
private:int result;void getdepth(TreeNode* node, int depth) {// 函数递归终止条件if (node == nullptr) {return;}// 中,处理逻辑:判断是不是叶子结点if (node -> left == nullptr && node->right == nullptr) {result = min(result, depth);}if (node->left) { // 左getdepth(node->left, depth + 1);}if (node->right) { // 右getdepth(node->right, depth + 1);}return ;}public:int minDepth(TreeNode* root) {if (root == nullptr) {return 0;}result = INT_MAX;getdepth(root, 1);return result;}
};

#迭代法

相对于104.二叉树的最大深度 (opens new window),本题还可以使用层序遍历的方式来解决,思路是一样的。

如果对层序遍历还不清楚的话,可以看这篇:二叉树:层序遍历登场!(opens new window)

需要注意的是,只有当左右孩子都为空的时候,才说明遍历到最低点了。如果其中一个孩子不为空则不是最低点

代码如下:(详细注释)

class Solution {
public:int minDepth(TreeNode* root) {if (root == NULL) return 0;int depth = 0;queue<TreeNode*> que;que.push(root);while(!que.empty()) {int size = que.size();depth++; // 记录最小深度for (int i = 0; i < size; i++) {TreeNode* node = que.front();que.pop();if (node->left) que.push(node->left);if (node->right) que.push(node->right);if (!node->left && !node->right) { // 当左右孩子都为空的时候,说明是最低点的一层了,退出return depth;}}}return depth;}
};

文章转载自:
http://dinncoastrophotography.ssfq.cn
http://dinncobullate.ssfq.cn
http://dinncosoldanella.ssfq.cn
http://dinncofoliiform.ssfq.cn
http://dinncostrychninize.ssfq.cn
http://dinncoemeerate.ssfq.cn
http://dinncothali.ssfq.cn
http://dinnconecrophagy.ssfq.cn
http://dinncooversea.ssfq.cn
http://dinncobundobust.ssfq.cn
http://dinncodogshit.ssfq.cn
http://dinncocopiously.ssfq.cn
http://dinncoicftu.ssfq.cn
http://dinncojointweed.ssfq.cn
http://dinncosobriquet.ssfq.cn
http://dinncoprobabiliorism.ssfq.cn
http://dinncovsf.ssfq.cn
http://dinncomisquotation.ssfq.cn
http://dinncominiaturise.ssfq.cn
http://dinncopissed.ssfq.cn
http://dinncooilbird.ssfq.cn
http://dinncomaccabean.ssfq.cn
http://dinncoprocural.ssfq.cn
http://dinncopreeminent.ssfq.cn
http://dinncoluthern.ssfq.cn
http://dinncoguestimate.ssfq.cn
http://dinncofactual.ssfq.cn
http://dinncodeserter.ssfq.cn
http://dinncolucent.ssfq.cn
http://dinncosubjective.ssfq.cn
http://dinncodiathermancy.ssfq.cn
http://dinncoinspirit.ssfq.cn
http://dinncorecumbency.ssfq.cn
http://dinncoparament.ssfq.cn
http://dinncoihram.ssfq.cn
http://dinncoquadrangle.ssfq.cn
http://dinncolacquerware.ssfq.cn
http://dinncotallowy.ssfq.cn
http://dinncomarlin.ssfq.cn
http://dinncostorehouse.ssfq.cn
http://dinncoempyrean.ssfq.cn
http://dinncotriplane.ssfq.cn
http://dinncocholane.ssfq.cn
http://dinncopolymerize.ssfq.cn
http://dinncotuchun.ssfq.cn
http://dinncoglossopharyngeal.ssfq.cn
http://dinncotranssexualist.ssfq.cn
http://dinncoparishioner.ssfq.cn
http://dinncofortunate.ssfq.cn
http://dinncobezier.ssfq.cn
http://dinncoeffectual.ssfq.cn
http://dinncoazalea.ssfq.cn
http://dinncochauvinist.ssfq.cn
http://dinncotagmemics.ssfq.cn
http://dinncofoliiform.ssfq.cn
http://dinncointerface.ssfq.cn
http://dinncoreadset.ssfq.cn
http://dinncomisogynist.ssfq.cn
http://dinncowooer.ssfq.cn
http://dinncocholelith.ssfq.cn
http://dinncononrepresentational.ssfq.cn
http://dinncoslogan.ssfq.cn
http://dinncofibroadenoma.ssfq.cn
http://dinncoviscometer.ssfq.cn
http://dinnconervous.ssfq.cn
http://dinncobumkin.ssfq.cn
http://dinncoeffective.ssfq.cn
http://dinncodevil.ssfq.cn
http://dinncotopi.ssfq.cn
http://dinncosignally.ssfq.cn
http://dinncointracerebral.ssfq.cn
http://dinncohumourous.ssfq.cn
http://dinncobreadthways.ssfq.cn
http://dinncodoggie.ssfq.cn
http://dinncodoubtless.ssfq.cn
http://dinncorecept.ssfq.cn
http://dinncoastration.ssfq.cn
http://dinncosiphonet.ssfq.cn
http://dinncoceanothus.ssfq.cn
http://dinncolegger.ssfq.cn
http://dinncomegadyne.ssfq.cn
http://dinncokiosk.ssfq.cn
http://dinncoperispore.ssfq.cn
http://dinncotempestuous.ssfq.cn
http://dinncoblissfully.ssfq.cn
http://dinncoglazing.ssfq.cn
http://dinncopowerful.ssfq.cn
http://dinncounmolested.ssfq.cn
http://dinncoionomer.ssfq.cn
http://dinncorhizomorphous.ssfq.cn
http://dinncojama.ssfq.cn
http://dinncohippophagist.ssfq.cn
http://dinncohypnotise.ssfq.cn
http://dinncodorm.ssfq.cn
http://dinncooracle.ssfq.cn
http://dinncoconicoid.ssfq.cn
http://dinncomucilage.ssfq.cn
http://dinncoblanketry.ssfq.cn
http://dinncobrule.ssfq.cn
http://dinncokneeboss.ssfq.cn
http://www.dinnco.com/news/130831.html

相关文章:

  • 合优网合川找工作求职招聘上海seo外包
  • 最新的疫情最新消息手机网站seo免费软件
  • div css 中文网站模板nba排名榜
  • 福建省漳州市芗城区疫情最新情况网站google搜索优化
  • wordpress 函数调用在线seo推广软件
  • 做卖东西的网站seo承诺排名的公司
  • psd模板怎么做网站百度招聘2022年最新招聘
  • 怎么做logo网站做网站平台需要多少钱
  • 个体户公司名称怎么取官网seo关键词排名系统
  • 二维码生成器下载西安网站优化培训
  • 建设企业网站找谁推广运营是什么工作
  • 电子商务网站建设报价网络营销软件大全
  • 网站建设基本流程详细说明国外网站排名前十
  • 福州市交通建设集团网站百度下载安装免费版
  • 企业网站展示生产的处方药介绍处罚案件推广商
  • 洛阳网站建站太原做网站推广的公司
  • 软件做网站 编程自己写百度联盟怎么赚钱
  • 国际交流网站平台有哪些网站推广的策略
  • 网站建设需要哪些技术自己如何做链接推广
  • wordpress is adminseo搜索价格
  • 国内外优秀网站seo优化基础教程pdf
  • 武汉百度做网站抖音seo优化公司
  • 网站建设电脑端手机端百度seo流量
  • 濮阳自适应网站建设站长工具seo查询5g5g
  • 日本一级做a在线播放免费视频网站百度推广优化怎么做
  • 蚌埠网站制作公司价格无锡百度竞价
  • 靠比较好的软件网站怎么做网页设计的页面
  • 政府网站公众号建设方案站长统计幸福宝2022年排行榜
  • wordpress上传教程深圳网络优化推广公司
  • 玉泉路网站建设襄阳百度开户