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

【力扣每日一题】力扣106从中序和后序遍历序列构造二叉树

题目来源

力扣106从中序和后序遍历序列构造二叉树

题目概述

给定两个整数数组 inorder 和 postorder ,其中 inorder 是二叉树的中序遍历, postorder 是同一棵树的后序遍历,请你构造并返回这颗 二叉树 。

思路分析

后序遍历序列的最末尾数据为树的根节点。 在中序遍历序列中找到树的根节点就可以找到这棵树的左子树范围和右子树范围。 分析方法与从前序与中序遍历序列构造二叉树类似。

代码实现

java实现

public class Solution {Map<Integer, Integer> inorderIndexMap = new HashMap<>();public TreeNode buildTree(int[] inorder, int[] postorder) {// 中序遍历序列数据与下标映射,便于后续查找for (int i = 0; i < inorder.length; i++) {inorderIndexMap.put(inorder[i],i);}return create(inorder, postorder ,0, inorder.length - 1, 0, postorder.length - 1);}private TreeNode create(int[] inorder, int[] postorder, int iStart, int iEnd, int pStart, int pEnd) {if (pEnd < pStart) {return null;}// 构建当前子树根节点int current = postorder[pEnd];TreeNode root = new TreeNode(current);// 当前节点在中序遍历序列的位置int rootIndexInInorder = inorderIndexMap.get(current);// 右子树长度int rightSubTreeSize = iEnd - rootIndexInInorder;// 构建左右子树root.right = create(inorder,postorder, rootIndexInInorder + 1, iEnd ,pEnd - rightSubTreeSize, pEnd - 1);root.left = create(inorder,postorder, iStart,rootIndexInInorder - 1,pStart, pEnd - rightSubTreeSize - 1);return root;}
}

c++实现

class Solution {
public:unordered_map<int, int> inorder_data_and_index;TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {// 中序遍历序列数据与下标映射,便于后续查找for (int i = 0; i < inorder.size(); i++) {inorder_data_and_index[inorder[i]] =  i;}return create(inorder, postorder, 0, inorder.size() - 1, 0, postorder.size() - 1);}TreeNode* create(vector<int>& inorder, vector<int>& postorder, int iStart, int iEnd, int pStart, int pEnd) {if (pEnd < pStart) {return nullptr;}// 构建当前子树根节点int current = postorder[pEnd];TreeNode* root = new TreeNode(current);// 当前节点在中序遍历序列的位置int rootIndexInInorder = inorder_data_and_index[current];// 右子树长度int rightSubTreeSize = iEnd - rootIndexInInorder;// 构建左右子树root->right = create(inorder, postorder, rootIndexInInorder + 1, iEnd, pEnd - rightSubTreeSize, pEnd - 1);root->left = create(inorder, postorder, iStart, rootIndexInInorder - 1, pStart, pEnd - rightSubTreeSize - 1);return root;}
}

http://www.lryc.cn/news/305108.html

相关文章:

  • logback日志回滚原理
  • [C#]winform基于opencvsharp结合pairlie算法实现低光图像增强黑暗图片变亮变清晰
  • React18源码: reconcliler启动过程
  • 【RN】为项目使用React Navigation中的navigator
  • CS50x 2024 - Lecture 8 - HTML, CSS, JavaScript
  • C++:派生类的生成过程(构造、析构)
  • 金蝶字段添加过滤条件
  • SQLite 知识整理
  • 0基础JAVA期末复习最终版
  • 【办公类-16-07-04】合并版“2023下学期 中班户外游戏(有场地和无场地版,一周一次)”(python 排班表系列)
  • chat GPT第一讲
  • JAVA工程师面试专题-Mysql篇
  • vue中使用echarts绘制双Y轴图表时,刻度没有对齐的两种解决方法
  • 编程笔记 Golang基础 022 数组
  • 【kubernetes】二进制部署k8s集群之,多master节点负载均衡以及高可用(下)
  • 哈希表在Java中的使用和面试常见问题
  • LeetCode刷题小记 三、【哈希表】
  • Zookeeper选举Leader源码剖析
  • Redis(十六)缓存预热+缓存雪崩+缓存击穿+缓存穿透
  • [已解决]npm淘宝镜像最新官方指引(2023.08.31)
  • ffmpeg之avformat_alloc_output_context2
  • GitLab代码库提交量统计工具
  • Python爬虫技术详解:从基础到高级应用,实战与应对反爬虫策略【第93篇—Python爬虫】
  • 关于TypeReference的使用
  • 阿里大文娱前端一面
  • Clickhouse系列之连接工具连接、数据类型和数据库
  • 【深入理解设计模式】原型设计模式
  • Python算法题集_图论(课程表)
  • 视频评论挖掘软件|抖音视频下载工具
  • Linux学习方法-框架学习法——Linux驱动架构的演进