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

hot100 -- 8.二叉树系列

1.二叉树的中序遍历

问题:给定一个二叉树的根节点 root ,返回 它的 中序 遍历 。

# 二叉树的中序遍历
import collectionsclass TreeNode:def __init__(self, val=0, left=None, right=None):self.val = valself.left = leftself.right = right# 创建二叉树
def CreateTree(values, index):if index >= len(values):return Nonereturn TreeNode(val=values[index], left=CreateTree(values, 2*index+1), right=CreateTree(values, 2*index+2))
#                       值               左孩子                             右孩子# 遍历二叉树(中序)
def Print(root):if not root:returnPrint(root.left)print(root.val)Print(root.right)# # 遍历二叉树(层序)
# def Print(root):
#     queue = collections.deque()
#     queue.append(root)
#     while queue:
#         queue_len = len(queue)
#         for i in range(queue_len):
#             node = queue.popleft()
#             if node:
#                 queue.append(node.left)
#                 queue.append(node.right)
#                 print(node.val)
#     returnroot = CreateTree([1, 2, 3, 4, None, 6], 0)
Print(root)
# print(root.right.left)

http://www.xdnf.cn/news/908317.html

相关文章:

  • 3D Web轻量化引擎HOOPS Communicator的定制化能力全面解析
  • LlamaIndex 工作流简介以及基础工作流
  • Linux驱动:class_create、device_create
  • java面试场景题:电商平台中订单未⽀付过期如何实现⾃动关单
  • 本地部署企业邮箱,让企业办公更安全高效
  • 【51单片机】0. 基础软件安装
  • Blazor-表单提交的艺术:如何优雅地实现 (下)
  • WorldExplorer:基于文本生成的可探索3D虚拟世界
  • 深克隆java对象的方式
  • 基于 openEuler 22.03 LTS SP1 构建 DPDK 22.11.8 开发环境指南
  • Xshell 详细安装与配置教程:从下载到高效使用
  • error: subprocess-exited-with-error【已解决】
  • docker 部署redis集群 配置
  • 【学习笔记】单例类模板
  • 深入理解二叉搜索树:原理到实践
  • libGL error
  • IDEA安装迁移IDEA配置数据位置
  • SQL进阶之旅 Day 19:统计信息与优化器提示
  • 10个成功案例剖析|融质AI创新实践
  • 【多线程初阶】阻塞队列 生产者消费者模型
  • Python备忘
  • CST人工电源网络阻抗计量校准
  • Python打卡训练营学习记录Day46
  • Arch-hyprland常用配置
  • 【Algo】常见组合类数列
  • 在centos7.9重置qcow2 root密码-qcow2忘记密码
  • 《0/1背包》题集
  • 【大厂机试题解法笔记】最差产品奖
  • 大模型编程助手-windsurf
  • 云服务器厂商机房是什么