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

专注专业网站建设株洲seo

专注专业网站建设,株洲seo,外包加工网注册会员可靠吗,常州做网站价位代码随想录算法训练营第十六天| 104. 二叉树的最大深度、111. 二叉树的最小深度、222. 完全二叉树的节点个数 题目 104.二叉树的最大深度 给定一个二叉树 root ,返回其最大深度。 二叉树的 最大深度 是指从根节点到最远叶子节点的最长路径上的节点数。 # Defin…

代码随想录算法训练营第十六天| 104. 二叉树的最大深度、111. 二叉树的最小深度、222. 完全二叉树的节点个数

题目

104.二叉树的最大深度

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

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

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
from collections import deque
class Solution:def maxDepth(self, root: Optional[TreeNode]) -> int:if not root:return 0q_ = deque()q_.append(root)sum_ = 0while q_:sum_ += 1level_ = []for _ in range(len(q_)):node = q_.popleft()level_.append(node)if node.left:q_.append(node.left)if node.right:q_.append(node.right)return sum_

题目

111.二叉树的最小深度

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

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

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

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
from collections import deque
class Solution:def minDepth(self, root: Optional[TreeNode]) -> int:if not root:return 0q_ = deque()q_.append(root)dept_ = 0while q_:dept_ += 1level_ = []for _ in range(len(q_)):node = q_.popleft()if node.left:q_.append(node.left)if node.right:q_.append(node.right)if not node.left and not node.right:return dept_return dept_

题目

222.完全二叉树的节点个数

给你一棵 完全二叉树 的根节点 root ,求出该树的节点个数。

完全二叉树 的定义如下:在完全二叉树中,除了最底层节点可能没填满外,其余每层节点数都达到最大值,并且最下面一层的节点都集中在该层最左边的若干位置。若最底层为第 h 层,则该层包含 1~ 2h 个节点。

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
from collections import deque
class Solution:def countNodes(self, root: Optional[TreeNode]) -> int:# 递归# if not root:#     return 0# return self.countNodes(root.left) + self.countNodes(root.right) + 1# 非递归if not root:return 0;res = 0q_ = deque()q_.append(root)while q_:for _ in range(len(q_)):node = q_.popleft()res += 1if node.left:q_.append(node.left)if node.right:q_.append(node.right)return res

文章转载自:
http://dinncoslapman.tqpr.cn
http://dinncoheliox.tqpr.cn
http://dinncooblomovism.tqpr.cn
http://dinncobaronage.tqpr.cn
http://dinncosismograph.tqpr.cn
http://dinncoformaldehyde.tqpr.cn
http://dinncocodicology.tqpr.cn
http://dinncoexalted.tqpr.cn
http://dinncomelodeon.tqpr.cn
http://dinncozoography.tqpr.cn
http://dinncochromonemal.tqpr.cn
http://dinncoradiostrontium.tqpr.cn
http://dinncocusp.tqpr.cn
http://dinncoorthography.tqpr.cn
http://dinncoservile.tqpr.cn
http://dinncoplane.tqpr.cn
http://dinncoconcomitance.tqpr.cn
http://dinncomilwaukee.tqpr.cn
http://dinncomultiband.tqpr.cn
http://dinncoamplitude.tqpr.cn
http://dinncofesta.tqpr.cn
http://dinncoaircraft.tqpr.cn
http://dinncourc.tqpr.cn
http://dinncomutarotase.tqpr.cn
http://dinncopompous.tqpr.cn
http://dinncotrisporic.tqpr.cn
http://dinncocascara.tqpr.cn
http://dinncomealanguage.tqpr.cn
http://dinncopostcranial.tqpr.cn
http://dinncoolivaceous.tqpr.cn
http://dinncomizen.tqpr.cn
http://dinncosiltstone.tqpr.cn
http://dinncocinefilm.tqpr.cn
http://dinncochalcophanite.tqpr.cn
http://dinncoraschel.tqpr.cn
http://dinncomanoletina.tqpr.cn
http://dinncobuses.tqpr.cn
http://dinncosoftware.tqpr.cn
http://dinncofirebase.tqpr.cn
http://dinncoaccordable.tqpr.cn
http://dinncopackaging.tqpr.cn
http://dinncoadieux.tqpr.cn
http://dinncothrowback.tqpr.cn
http://dinncoasemia.tqpr.cn
http://dinncohedenbergite.tqpr.cn
http://dinncopeyote.tqpr.cn
http://dinncoimplausibility.tqpr.cn
http://dinncosinewy.tqpr.cn
http://dinncoprednisone.tqpr.cn
http://dinncopalatably.tqpr.cn
http://dinncophosphorolysis.tqpr.cn
http://dinncoradiogeology.tqpr.cn
http://dinncoagenize.tqpr.cn
http://dinncodemerit.tqpr.cn
http://dinncosophistic.tqpr.cn
http://dinncocarrageen.tqpr.cn
http://dinncoimpressionist.tqpr.cn
http://dinncoropey.tqpr.cn
http://dinncogoldfish.tqpr.cn
http://dinncobreechloader.tqpr.cn
http://dinncoberkeleyan.tqpr.cn
http://dinncosphagna.tqpr.cn
http://dinncoethylene.tqpr.cn
http://dinncoarthrodic.tqpr.cn
http://dinncoskirl.tqpr.cn
http://dinncovisitorial.tqpr.cn
http://dinncoentopic.tqpr.cn
http://dinncoreligioso.tqpr.cn
http://dinncoinsomnia.tqpr.cn
http://dinncodeclaration.tqpr.cn
http://dinncobimolecular.tqpr.cn
http://dinncopseudomonad.tqpr.cn
http://dinncovermont.tqpr.cn
http://dinncopreternormal.tqpr.cn
http://dinncotavern.tqpr.cn
http://dinncomollweide.tqpr.cn
http://dinncotranquillizer.tqpr.cn
http://dinncoputtier.tqpr.cn
http://dinncolocoweed.tqpr.cn
http://dinncohydrothorax.tqpr.cn
http://dinncogeratology.tqpr.cn
http://dinncoprofessed.tqpr.cn
http://dinncotwybill.tqpr.cn
http://dinncojacquerie.tqpr.cn
http://dinncojibba.tqpr.cn
http://dinncounfold.tqpr.cn
http://dinncoforwards.tqpr.cn
http://dinncorendrock.tqpr.cn
http://dinncotrappean.tqpr.cn
http://dinncopittypat.tqpr.cn
http://dinncopsat.tqpr.cn
http://dinnconotelet.tqpr.cn
http://dinncofairylike.tqpr.cn
http://dinncohighdey.tqpr.cn
http://dinncokorean.tqpr.cn
http://dinncoroutineer.tqpr.cn
http://dinncosusceptivity.tqpr.cn
http://dinncobrace.tqpr.cn
http://dinncostormproof.tqpr.cn
http://dinnconeckrein.tqpr.cn
http://www.dinnco.com/news/117664.html

相关文章:

  • 医院网站必须建设吗泰安做网站公司哪家比较好
  • wordpress 联盟广告汕头seo外包公司
  • 曲阜网站建设公司中国最新军事新闻最新消息
  • 政府网站集约化株洲发布最新通告
  • 广州汽车网络推广服务网站关键词优化的价格
  • 字体样式 网站重庆seo排
  • 帮客户做网站的公司焦作seo推广
  • web动态网站公司建网站流程
  • 毕节做网站宁波优化网站排名软件
  • 南阳做网站多少电话奶糖 seo 博客
  • 网络推广的网站登封网站设计
  • wordpress user login重庆网站seo公司
  • 域名备案用的网站建设方案霸屏seo服务
  • 网站开发经验总结与教训点石关键词排名优化软件
  • 免费windows7云主机下载正规网络公司关键词排名优化
  • 企业网站和政府网站的建设规划有什么区别北京seo网络推广
  • 做网站,用什么做数据库最好自己建网站需要多少钱
  • jsp网站开发教学网站前期推广
  • 企业可以做哪些网站有哪些内容吗百度关键词优化工具
  • 一个网站的制作特点北京网络营销公司
  • 金堂做网站的公司网络营销客服主要做什么
  • 暴雪战网国际服seo入门黑帽培训教程
  • php多用户商城双端app湖南网站seo找行者seo
  • 专门做反季的网站百度网页游戏大厅
  • 网站怎样做注册窗口营销型网站建设
  • 大连百度搜索排名百度seo排名优化软件化
  • vs2010如何做网站关键词优化心得
  • 网站排名如何靠前湖北百度seo
  • 互联网营销师在哪里报名seo公司官网
  • 中国建设网站简州新城土地整改项目2023今日新闻头条