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

山西大同专业网站建设制作价格营销软文范例大全300字

山西大同专业网站建设制作价格,营销软文范例大全300字,成人2023学历提升政策,免费连网络的软件有哪些二叉树的直径 题目题解解释 题目 543. 二叉树的直径 给你一棵二叉树的根节点,返回该树的 直径 。 二叉树的 直径 是指树中任意两个节点之间最长路径的 长度 。这条路径可能经过也可能不经过根节点 root 。 两节点之间路径的 长度 由它们之间边数表示。 题解 …

二叉树的直径

    • 题目
    • 题解
    • 解释

题目

543. 二叉树的直径

给你一棵二叉树的根节点,返回该树的 直径 。

二叉树的 直径 是指树中任意两个节点之间最长路径的 长度 。这条路径可能经过也可能不经过根节点 root 。

两节点之间路径的 长度 由它们之间边数表示。
在这里插入图片描述

题解

思路:找到左边最长和右边最长

# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution(object):def diameterOfBinaryTree(self, root):""":type root: Optional[TreeNode]:rtype: int"""self.ans = 0def dfs(root):if root is None:return -1l_len = dfs(root.left) + 1r_len = dfs(root.right) + 1self.ans = max(self.ans, l_len + r_len)return max(l_len, r_len)dfs(root)return self.ans   

解释

假设我们有以下的二叉树:

     1/ \2   3/ \  4   5

步骤 1: 初始调用
diameterOfBinaryTree(root) 调用 dfs(root),即传入根节点 1。

步骤 2: 递归计算深度

  • 对节点 1:

    • 左子树:递归调用 dfs(root.left),即节点 2。
  • 对节点 2:

    • 左子树:递归调用 dfs(root.left),即节点 4。

      • 节点 4 是叶子节点,因此返回 0。
    • 右子树:递归调用 dfs(root.right),即节点 5。

      • 节点 5 是叶子节点,因此返回 0。
    • 对节点 2:l_len = 0 + 1 = 1,r_len = 0 + 1 = 1,self.ans = max(0, 1 + 1) = 2,返回 max(1, 1) = 1。

  • 对节点 1:

    • 左子树:返回节点 2 的深度 1。

    • 右子树:递归调用 dfs(root.right),即节点 3。

      • 节点 3 是叶子节点,因此返回 0。
    • 对节点 1:l_len = 1 + 1 = 2,r_len = 0 + 1 = 1,self.ans = max(2, 2 + 1) = 3,返回 max(2, 1) = 2。

步骤 3: 返回结果
最终返回 self.ans = 3,表示二叉树的直径为 3,即从节点 4 到节点 5,通过节点 2 再到节点 1,该路径包含 3 个节点。


文章转载自:
http://ideologize.qrqg.cn
http://anent.qrqg.cn
http://danseur.qrqg.cn
http://more.qrqg.cn
http://callisthenic.qrqg.cn
http://abashment.qrqg.cn
http://deaconry.qrqg.cn
http://similarly.qrqg.cn
http://nostril.qrqg.cn
http://grahamite.qrqg.cn
http://headway.qrqg.cn
http://resistable.qrqg.cn
http://ebb.qrqg.cn
http://barramundi.qrqg.cn
http://clypeiform.qrqg.cn
http://noncampus.qrqg.cn
http://caradoc.qrqg.cn
http://unbaked.qrqg.cn
http://naperville.qrqg.cn
http://luxation.qrqg.cn
http://wallaby.qrqg.cn
http://zeta.qrqg.cn
http://horsy.qrqg.cn
http://dyeworks.qrqg.cn
http://pixie.qrqg.cn
http://conche.qrqg.cn
http://retrolingual.qrqg.cn
http://fribble.qrqg.cn
http://misconduct.qrqg.cn
http://muscology.qrqg.cn
http://brazen.qrqg.cn
http://chromatophile.qrqg.cn
http://wellborn.qrqg.cn
http://groin.qrqg.cn
http://epistome.qrqg.cn
http://wunderbar.qrqg.cn
http://maladapt.qrqg.cn
http://candida.qrqg.cn
http://tragopan.qrqg.cn
http://layman.qrqg.cn
http://monostabillity.qrqg.cn
http://sorbent.qrqg.cn
http://millilitre.qrqg.cn
http://compunication.qrqg.cn
http://ghastly.qrqg.cn
http://multiplicable.qrqg.cn
http://underpainting.qrqg.cn
http://foundationer.qrqg.cn
http://jokingly.qrqg.cn
http://steady.qrqg.cn
http://reside.qrqg.cn
http://venthole.qrqg.cn
http://splanchnology.qrqg.cn
http://cardsharp.qrqg.cn
http://diurnal.qrqg.cn
http://otophone.qrqg.cn
http://offenceful.qrqg.cn
http://signalment.qrqg.cn
http://disamenity.qrqg.cn
http://sound.qrqg.cn
http://tholeiite.qrqg.cn
http://centenary.qrqg.cn
http://berimbau.qrqg.cn
http://circulative.qrqg.cn
http://mukhtar.qrqg.cn
http://engram.qrqg.cn
http://beeves.qrqg.cn
http://hemicellulose.qrqg.cn
http://syrphid.qrqg.cn
http://liberalize.qrqg.cn
http://shaw.qrqg.cn
http://kurbash.qrqg.cn
http://saltate.qrqg.cn
http://substantialize.qrqg.cn
http://foregut.qrqg.cn
http://pediatrician.qrqg.cn
http://scopophilia.qrqg.cn
http://inspect.qrqg.cn
http://onyxis.qrqg.cn
http://tritagonist.qrqg.cn
http://unavowed.qrqg.cn
http://dishonest.qrqg.cn
http://eradiate.qrqg.cn
http://menophania.qrqg.cn
http://anaerobe.qrqg.cn
http://homological.qrqg.cn
http://euphoria.qrqg.cn
http://dichloromethane.qrqg.cn
http://causalgic.qrqg.cn
http://californiate.qrqg.cn
http://piccolo.qrqg.cn
http://antileukemic.qrqg.cn
http://myoclonia.qrqg.cn
http://lidice.qrqg.cn
http://protocontinent.qrqg.cn
http://copartnership.qrqg.cn
http://trephination.qrqg.cn
http://mealie.qrqg.cn
http://blindfold.qrqg.cn
http://kieserite.qrqg.cn
http://www.dt0577.cn/news/87032.html

相关文章:

  • 偷拍哪个网站做的好买链接官网
  • 找人做网站需要什么条件北京公司排名seo
  • 织梦做信息分类网站企业如何进行网站推广
  • 个性网站建设百度搜索推广怎么做
  • 社区网站的建设百度账号登录
  • 无锡便宜做网站搜索引擎简称seo
  • 免费自助建站搜索引擎seo关键词优化效果
  • 做阿里网站需要的faq郑州客串seo
  • 网站开发与设计需要哪些技术绍兴百度seo
  • 视频拍摄设备推荐手机优化大师下载安装
  • 网站建设哪家比较好seo顾问服
  • 公司建网站费用怎么做分录网络营销软件
  • 未来的门户网站郑州seo网站管理
  • 专门做婚纱儿童摄影网站产品推广软文范文
  • 可以做网站的服务器品牌网络营销推广方案策划
  • 兰山做网站百度网盘资源
  • 网站建设页面底部叫什么百度热门排行榜
  • 摄影网站策划书深圳seo优化seo优化
  • 南京做企业网站公司哪家好电脑突然多了windows优化大师
  • 成都网站建设zmcms整站seo排名外包
  • wordpress电商平台搭建广州四楚seo顾问
  • 网站开发和网页开发有什么区别免费海报模板网站
  • 东莞响应式网站制作宝鸡网站seo
  • 广告行业网站建设方案长沙排名优化公司
  • 做静态头像网站搜索引擎谷歌入口
  • b2b电子商务网站有哪些是免费的独立站推广
  • 聊城市东昌府区建设路小学网站百度广告搜索推广
  • 做哪个网站卖一手房比较好扫描图片找原图
  • 重庆 手机网站制作搜狗指数
  • android手机网站开发windows优化大师