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

Python | Leetcode Python题解之第105题从前序与中序遍历序列构造二叉树

题目:

题解:

class Solution:def buildTree(self, preorder: List[int], inorder: List[int]) -> TreeNode:if not preorder:return Noneroot = TreeNode(preorder[0])stack = [root]inorderIndex = 0for i in range(1, len(preorder)):preorderVal = preorder[i]node = stack[-1]if node.val != inorder[inorderIndex]:node.left = TreeNode(preorderVal)stack.append(node.left)else:while stack and stack[-1].val == inorder[inorderIndex]:node = stack.pop()inorderIndex += 1node.right = TreeNode(preorderVal)stack.append(node.right)return root
http://www.lryc.cn/news/358228.html

相关文章:

  • 经典面试题:什么是事物的隔离级别?什么是MVCC?
  • Java程序中,不同jar包,有2个完全相同的类,运行时的加载顺序
  • EI期刊的定金和尾款
  • python+pytest+pytest-html+allure集成测试案例
  • STL-priority_queue的使用及其模拟实现
  • pycharm连接阿里云服务器过程记录
  • 移动硬盘未格式化数据恢复及预防策略
  • MySQL数据库入门之视图、存储过程、触发器
  • Kafka原生API使用Java代码-生产者-分区策略-默认分区策略轮询分区策略
  • 网页中的音视频裁剪拼接合并
  • 【入门】使用sklearn实现的KNN算法:鸢尾花数据集分类预测
  • nss做题
  • ​第18章:JDK8-17新特性
  • 哈希表练习题(2024/5/29)
  • java —— 连接 MySQL 操作
  • 从 0 开始实现一个博客系统 (SSM 项目)
  • C++标准模板(STL)- C 内存管理库 - 分配并清零内存 (std::calloc)
  • 嵌入式开发面试问题总结(持续更新)
  • 意外发现openGauss兼容Oracle的几个条件表达式
  • 使用Keepalived提高吞吐量和负载均衡ip_hash.
  • 网络故障与排除(一)
  • C++之运算符重载
  • 使用springdoc-openapi-starter-webmvc-ui后访问swagger-ui/index.html 报错404
  • 深入理解计算机系统 家庭作业4.52
  • 深度学习:手撕 RNN(2)-RNN 的常见模型架构
  • 【Linux进程篇】Linux进程管理——进程创建与终止
  • Python爬虫实战(实战篇)—17获取【CSDN某一专栏】数据转为Markdown列表放入文章中
  • Go语言-big.Int
  • getContentView(mBinding.getRoot()); 会导致内存泄露吗?里面有SurfaceView ViewBinding
  • 基于transformers框架实践Bert系列6-完形填空