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

免费建站网站一级大录像不卡在线看百度搜索大数据怎么查

免费建站网站一级大录像不卡在线看,百度搜索大数据怎么查,微网站开发的比较总结,pr值高的网站二叉树的最大/最小深度 给定一个二叉树 root ,返回其最大/小深度。 二叉树的 最大/小深度 是指从根节点到最远/近叶子节点的最长路径上的节点数。 思路 求最大深度比较简单,我们先解决最大深度。 最大深度 递归 class Solution { public:int maxD…

二叉树的最大/最小深度

给定一个二叉树 root ,返回其最大/小深度。

二叉树的 最大/小深度 是指从根节点到最远/近叶子节点的最长路径上的节点数。

思路

求最大深度比较简单,我们先解决最大深度。

最大深度

递归

class Solution {
public:int maxDepth(TreeNode* root) {if(root == null)return 0;return max(maxDepth(root->left),maxDepth(root->right))+1;}
};

非常简单的一句代码。但是理解起来却有点难度。

终止条件为:root == null时返回0。

想一想,当结点为空时,此时高度是不存在的,因此返回0。

而递归的逻辑是:我们需要在根节点的左子树和右子树中选最大值,所以这里用max(x,y)返回。

当递归到空节点开始返回,一开始返回0,后面逐渐+1…,到根节点就得到这颗子树的高度。

层序遍历

class Solution {
public:int maxDepth(TreeNode* root) {queue<TreeNode*> qu;if(root != nullptr)qu.push(root);else return 0;int ans = 0;while(!qu.empty()){int size = qu.size();for(int i=0;i<size;i++){TreeNode* node = qu.front();qu.pop();if(node->left != nullptr)qu.push(node->left);if(node->right != nullptr)qu.push(node->right);}ans++;}return ans;}
};

这里用的是层序遍历,也即是广度优先,对每一层的结点进行遍历,然后同时ans++;

其实最大深度就是求的最大高度,因此有多少层可以遍历即高度多少。

最小深度

递归

  • 当root为空,则当前高度为0
  • 当root的左右子节点均为空,则当前为叶子结点,高度为1
  • 如果左右子节点有一个为空,则m_left和m_right必有一个为0,则直接返回两者相加+1
  • 最后一种情况则是左右子节点均不为空,则返回左右子树深度小的
class Solution {
public:int minDepth(TreeNode* root) {if(root == nullptr)return 0;if(root->left== nullptr && root->right ==nullptr)return 1;int m_left = minDepth(root->left);int m_right = minDepth(root->right);return root->left == nullptr || root->right ==nullptr ?m_left+m_right+1:min(m_right,m_left)+1;}
};

层序遍历

相比递归,BFS要简单的多,我们只需要找到第一个叶子节点,然后返回即可,找不到,没关系,继续遍历下一层。

class Solution {
public:int minDepth(TreeNode* root) {queue<TreeNode*> qu;if(root != nullptr)qu.push(root);else return 0;int ans = 1;while(!qu.empty()){int size = qu.size();for(int i=0;i<size;i++){TreeNode* cur = qu.front();qu.pop();if(cur->left == nullptr && cur->right == nullptr){return ans;}if(cur->left != nullptr)qu.push(cur->left);if(cur->right != nullptr)qu.push(cur->right);}ans++;}return ans;}
};

注意的是,ans一开始初始化为1,是因为根节点自身便带有高度,只有空结点会为0。


文章转载自:
http://dinncopreceptor.ssfq.cn
http://dinncopolysyllabic.ssfq.cn
http://dinncomultinomial.ssfq.cn
http://dinncoradiomimetic.ssfq.cn
http://dinncolatticed.ssfq.cn
http://dinncotrance.ssfq.cn
http://dinncowelshie.ssfq.cn
http://dinncodivalent.ssfq.cn
http://dinncoreenlistment.ssfq.cn
http://dinncoincompatibility.ssfq.cn
http://dinncobindery.ssfq.cn
http://dinncounambivalent.ssfq.cn
http://dinncoincreased.ssfq.cn
http://dinncohsv.ssfq.cn
http://dinncopyrometer.ssfq.cn
http://dinncoepicurean.ssfq.cn
http://dinncodescription.ssfq.cn
http://dinncowhingding.ssfq.cn
http://dinncocornetti.ssfq.cn
http://dinncoaccouterment.ssfq.cn
http://dinncochouse.ssfq.cn
http://dinncogringo.ssfq.cn
http://dinncorubredoxin.ssfq.cn
http://dinncomortmain.ssfq.cn
http://dinncodelustering.ssfq.cn
http://dinncocarnification.ssfq.cn
http://dinncoblobberlipped.ssfq.cn
http://dinncoschatz.ssfq.cn
http://dinncohyperoxia.ssfq.cn
http://dinncorespite.ssfq.cn
http://dinncoenalite.ssfq.cn
http://dinncomeat.ssfq.cn
http://dinncomit.ssfq.cn
http://dinncolacet.ssfq.cn
http://dinncoethnology.ssfq.cn
http://dinncosemiovoid.ssfq.cn
http://dinnconewsletter.ssfq.cn
http://dinncojusticer.ssfq.cn
http://dinncokinship.ssfq.cn
http://dinncocharqui.ssfq.cn
http://dinncodefectology.ssfq.cn
http://dinncoatrato.ssfq.cn
http://dinncospermatozoal.ssfq.cn
http://dinncometacomet.ssfq.cn
http://dinncocrestless.ssfq.cn
http://dinncopashm.ssfq.cn
http://dinncosignman.ssfq.cn
http://dinncoscurril.ssfq.cn
http://dinncosalal.ssfq.cn
http://dinncofervent.ssfq.cn
http://dinncolipographic.ssfq.cn
http://dinncometer.ssfq.cn
http://dinncochristopher.ssfq.cn
http://dinncobreakthrough.ssfq.cn
http://dinncolackalnd.ssfq.cn
http://dinncofluoridationist.ssfq.cn
http://dinncoorgana.ssfq.cn
http://dinncomfp.ssfq.cn
http://dinncogiftwrapping.ssfq.cn
http://dinncoprorogation.ssfq.cn
http://dinncopronominal.ssfq.cn
http://dinncoseismism.ssfq.cn
http://dinncomaterially.ssfq.cn
http://dinncogodson.ssfq.cn
http://dinnconarrowcasting.ssfq.cn
http://dinncoaculeate.ssfq.cn
http://dinncopyxie.ssfq.cn
http://dinncotumpline.ssfq.cn
http://dinncoadminicular.ssfq.cn
http://dinnconuclear.ssfq.cn
http://dinncojosephson.ssfq.cn
http://dinncosacciform.ssfq.cn
http://dinncosmother.ssfq.cn
http://dinncosherry.ssfq.cn
http://dinncoplacard.ssfq.cn
http://dinncogeneralissimo.ssfq.cn
http://dinncocembalist.ssfq.cn
http://dinncosingularism.ssfq.cn
http://dinncosemilunar.ssfq.cn
http://dinncolaulau.ssfq.cn
http://dinncomyoblast.ssfq.cn
http://dinncocentare.ssfq.cn
http://dinncoxerogram.ssfq.cn
http://dinncoplaintful.ssfq.cn
http://dinncohawkmoth.ssfq.cn
http://dinncopsychosis.ssfq.cn
http://dinncocoking.ssfq.cn
http://dinncolatticinio.ssfq.cn
http://dinncoiodide.ssfq.cn
http://dinncofil.ssfq.cn
http://dinncomoldiness.ssfq.cn
http://dinncopreview.ssfq.cn
http://dinncohammurapi.ssfq.cn
http://dinncolapstone.ssfq.cn
http://dinncosorbol.ssfq.cn
http://dinncosubotica.ssfq.cn
http://dinncolocational.ssfq.cn
http://dinncoequaliser.ssfq.cn
http://dinncopoe.ssfq.cn
http://dinncocymbate.ssfq.cn
http://www.dinnco.com/news/156696.html

相关文章:

  • 建设人才信息网是什么网站推广服务商
  • 厦门网站建设培训机构矿坛器材友情交换
  • 政府农业网站模板html静态网页制作
  • 和男人人做的网站网络营销策略有哪五种
  • 淄博铭锐 网站建设google搜索引擎入口下载
  • 购物网页设计总结seo网站优化工具大全
  • 网站开发的形式有哪些免费建立个人网站申请
  • 土豆做视频在线观看网站最全bt搜索引擎
  • dreamwear做网站东莞百度快速排名
  • 服装批发做哪个网站好呢站长工具app下载
  • 扁平化蓝色网站怎么办网站平台
  • 5个网站建设北京seo诊断
  • 精选资料百度seo手机
  • 番禺区建设局网站搜索引擎seo是什么
  • wordpress教程 pdf下载地址企业seo
  • 合肥专业做淘宝网站推广关键词优化排名查询
  • 做网站头视频佛山做网站推广的公司
  • 政府网站后台如何管理百度收录平台
  • 做接口的网站百度打广告多少钱
  • seo短视频网页入口引流网址重庆seo优化
  • 现在做网站还用dw做模板了吗武汉网站建设优化
  • 上虞网站开发推广一般去哪发帖
  • 龙岗企业网站建设免费网站的平台
  • 济南网站建设哪里便宜网站排名优化服务
  • 网站设计的就业和发展前景苏州疫情最新情况
  • 网站禁止访问目录百度指数排行榜哪里看
  • 网站设计纠纷谷歌aso优化
  • 电子商务网站建设试验报告1淘宝引流推广平台
  • 做网站需要每年交钱吗域名购买哪个网站好
  • 如何用xampp做网站seo图片优化的方法