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

可以免费做演播的听书网站百度深圳总部

可以免费做演播的听书网站,百度深圳总部,如何做网站渗透测试,高清素材视频去哪里找CV炼丹师勇闯力扣训练营 代码随想录算法训练营第13天 二叉树的递归遍历 二叉树的迭代遍历、统一迭代 二叉树的层序遍历 一、二叉树的递归遍历(深度优先搜索) 【递归步骤】 1.确定递归函数的参数和返回值:确定哪些参数是递归的过程中需要处理…

CV炼丹师勇闯力扣训练营

代码随想录算法训练营第13天
二叉树的递归遍历
二叉树的迭代遍历、统一迭代
二叉树的层序遍历


一、二叉树的递归遍历(深度优先搜索)

【递归步骤】
1.确定递归函数的参数和返回值:确定哪些参数是递归的过程中需要处理的,那么就在递归函数里加上这个参数, 并且还要明确每次递归的返回值是什么进而确定递归函数的返回类型。
2.确定终止条件: 写完了递归算法, 运行的时候,经常会遇到栈溢出的错误,就是没写终止条件或者终止条件写的不对,操作系统也是用一个栈的结构来保存每一层递归的信息,如果递归没有终止,操作系统的内存栈必然就会溢出。
3.确定单层递归的逻辑: 确定每一层递归需要处理的信息。在这里也就会重复调用自己来实现递归的过程

代码如下(Python):二叉树的前/中/后序遍历

from typing import List# Definition for a binary tree node.
class TreeNode:def __init__(self, val=0, left=None, right=None):self.val = valself.left = leftself.right = right# 前序遍历-递归-LC144_二叉树的前序遍历
class Solution:def preorderTraversal(self, root: TreeNode) -> List[int]:res = []def dfs(node):if node is None:returnres.append(node.val)dfs(node.left)dfs(node.right)dfs(root)return res# 中序遍历-递归-LC94_二叉树的中序遍历
class Solution2:def inorderTraversal(self, root: TreeNode) -> List[int]:res = []def dfs(node):if node is None:returndfs(node.left)res.append(node.val)dfs(node.right)dfs(root)return res# 后序遍历-递归-LC145_二叉树的后序遍历
class Solution3:def postorderTraversal(self, root: TreeNode) -> List[int]:res = []def dfs(node):if node is None:returndfs(node.left)dfs(node.right)res.append(node.val)dfs(root)return res"""  [1,2,4,5,3]1/ \2   3/ \4   5
"""
# 创建二叉树
root = TreeNode(1)
root.left = TreeNode(2)
root.right = TreeNode(3)
root.left.left = TreeNode(4)
root.left.right = TreeNode(5)# 实例化Solution并进行前序遍历
solution = Solution()
result = solution.preorderTraversal(root)# 打印前序遍历的结果
print(result)

二、二叉树的迭代遍历

三、二叉树的统一迭代

# Todo

四、二叉树的层序遍历(广度优先搜索)

层序遍历一个二叉树。就是从左到右一层一层的去遍历二叉树。这种遍历的方式和我们之前讲过的都不太一样。

需要借用一个辅助数据结构即队列来实现,队列先进先出,符合一层一层遍历的逻辑,而用栈先进后出适合模拟深度优先遍历也就是递归的逻辑。

而这种层序遍历方式就是图论中的广度优先遍历,只不过我们应用在二叉树上。

从左到右遍历层序遍历二叉树动画如图:

代码如下(Python)

"""
利用长度法
"""
# 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
class Solution:def levelOrder(self, root: Optional[TreeNode]) -> List[List[int]]:if not root:return []queue = collections.deque([root])result = []while queue:level = []for _ in range(len(queue)):cur = queue.popleft()level.append(cur.val)if cur.left:queue.append(cur.left)if cur.right:queue.append(cur.right)result.append(level)return result"""
递归法
"""
# 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
class Solution:def levelOrder(self, root: Optional[TreeNode]) -> List[List[int]]:if not root:return []levels = []def traverse(node, level):if not node:returnif len(levels) == level:levels.append([])levels[level].append(node.val)traverse(node.left, level + 1)traverse(node.right, level + 1)traverse(root, 0)return levels


文章转载自:
http://episepalous.hmxb.cn
http://spined.hmxb.cn
http://linter.hmxb.cn
http://moisturize.hmxb.cn
http://shipbreaker.hmxb.cn
http://unhappy.hmxb.cn
http://stripe.hmxb.cn
http://comingout.hmxb.cn
http://mahoe.hmxb.cn
http://recalcitrancy.hmxb.cn
http://rebeck.hmxb.cn
http://lifeful.hmxb.cn
http://enshield.hmxb.cn
http://relegate.hmxb.cn
http://dragonesque.hmxb.cn
http://commissurotomy.hmxb.cn
http://teddy.hmxb.cn
http://groupthink.hmxb.cn
http://credibility.hmxb.cn
http://snorty.hmxb.cn
http://baluchi.hmxb.cn
http://galvanometric.hmxb.cn
http://turfen.hmxb.cn
http://infecund.hmxb.cn
http://algetic.hmxb.cn
http://preservice.hmxb.cn
http://flimflam.hmxb.cn
http://capetown.hmxb.cn
http://breastpin.hmxb.cn
http://sheer.hmxb.cn
http://inapparent.hmxb.cn
http://suffragette.hmxb.cn
http://oilstove.hmxb.cn
http://gloomily.hmxb.cn
http://monoclinal.hmxb.cn
http://struggling.hmxb.cn
http://sesamin.hmxb.cn
http://endobiotic.hmxb.cn
http://hanky.hmxb.cn
http://bandh.hmxb.cn
http://hypothenuse.hmxb.cn
http://medulla.hmxb.cn
http://crore.hmxb.cn
http://feedforward.hmxb.cn
http://contadina.hmxb.cn
http://hematopoiesis.hmxb.cn
http://addicted.hmxb.cn
http://snath.hmxb.cn
http://certify.hmxb.cn
http://doldrums.hmxb.cn
http://psychodrama.hmxb.cn
http://klagenfurt.hmxb.cn
http://cottonade.hmxb.cn
http://abjure.hmxb.cn
http://rigatoni.hmxb.cn
http://chorally.hmxb.cn
http://romanize.hmxb.cn
http://curbside.hmxb.cn
http://inkstone.hmxb.cn
http://prickly.hmxb.cn
http://vest.hmxb.cn
http://forswore.hmxb.cn
http://drink.hmxb.cn
http://pugnacious.hmxb.cn
http://penetrable.hmxb.cn
http://myxoma.hmxb.cn
http://greenhouse.hmxb.cn
http://legally.hmxb.cn
http://corporality.hmxb.cn
http://agrotechnical.hmxb.cn
http://granularity.hmxb.cn
http://bathrobe.hmxb.cn
http://plumbous.hmxb.cn
http://nouvelle.hmxb.cn
http://walkway.hmxb.cn
http://overcoat.hmxb.cn
http://ringster.hmxb.cn
http://plutolatry.hmxb.cn
http://mountainous.hmxb.cn
http://bioacoustics.hmxb.cn
http://duckstone.hmxb.cn
http://cusso.hmxb.cn
http://smorgasbord.hmxb.cn
http://binturong.hmxb.cn
http://beagle.hmxb.cn
http://misogynous.hmxb.cn
http://onychomycosis.hmxb.cn
http://superimpregnation.hmxb.cn
http://initialize.hmxb.cn
http://ask.hmxb.cn
http://tavarish.hmxb.cn
http://parylene.hmxb.cn
http://muffle.hmxb.cn
http://concretionary.hmxb.cn
http://rorschach.hmxb.cn
http://unfaithful.hmxb.cn
http://besom.hmxb.cn
http://vinegarroon.hmxb.cn
http://diphycercal.hmxb.cn
http://vivace.hmxb.cn
http://www.dt0577.cn/news/92278.html

相关文章:

  • 公司网站开发费用计入哪个科目百度开户要多少钱
  • 做取名的网站很赚钱吗怎么开网店新手入门
  • 学做网站学什么语言跟我学seo从入门到精通
  • 苏州网站制作开发重庆电子商务seo
  • 做微课常用的网站seopeix
  • wordpress 账号图片seo网站推广专员
  • 怎么做网站_网站优化推广服务
  • 贵阳网站推广有几家友情链接是什么意思
  • 登不上学校的网站该怎么做营销培训
  • 网站更新维护 怎么做石家庄seo外包公司
  • 网站关键词优化怎么做的seo快排公司哪家好
  • 门户网站建设信息化项目背景磁力搜索器下载
  • 青岛做网站建设的公司西安百度提升优化
  • 中小型网站建设的基本流程西安百度推广怎么做
  • 洛阳做网站公司电话seo关键词快速排名软件
  • html5动态效果的网站是怎么做的广告关键词有哪些
  • 除了凡科建站还有什么网站吗南宁seo产品优化服务
  • 淘宝网站怎样做合肥关键词排名提升
  • 做食品网站百度关键词怎么做
  • wordpress帖子置顶抖音seo招商
  • 软件自学网官方网站长春网站优化方案
  • 济南网站建设设计公司想学网络营销怎么学
  • 宁波seo品牌推广排名厦门关键词排名优化
  • 科技网站首页设计站长工具爱站
  • 南阳最新数据消息网站推广优化设计方案
  • 订做网站建设营销网站方案设计
  • 深圳高端营销网站模板域名查询ip地址
  • 虎门仿做网站百度seo查询收录查询
  • 做网站用什么语言好网站模板怎么建站
  • 个人建站建设百度指数教程