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

106. 从中序与后序遍历序列构造二叉树【中等】

106. 从中序与后序遍历序列构造二叉树【中等

题目描述

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

示例 1:
输入:inorder = [9,3,15,20,7], postorder = [9,15,7,20,3]
输出:[3,9,20,null,null,15,7]

在这里插入图片描述

示例 2:
输入:inorder = [-1], postorder = [-1]
输出:[-1]

提示:

  • 1 <= inorder.length <= 3000
  • postorder.length == inorder.length
  • -3000 <= inorder[i], postorder[i] <= 3000
  • inorder 和 postorder 都由不同的值组成
  • postorder 中每一个值都在 inorder 中
  • inorder 保证是树的中序遍历
  • postorder 保证是树的后序遍历

代码

递归方法

思路:类似前序遍历中序遍历推理树的思路。指路LCR 124.推理二叉树

/*** Definition for a binary tree node.* public class TreeNode {*     int val;*     TreeNode left;*     TreeNode right;*     TreeNode() {}*     TreeNode(int val) { this.val = val; }*     TreeNode(int val, TreeNode left, TreeNode right) {*         this.val = val;*         this.left = left;*         this.right = right;*     }* }*/
class Solution {public TreeNode buildTree(int[] inorder, int[] postorder) {Map<Integer,Integer> map = new HashMap<Integer,Integer>();for(int i = 0;i<inorder.length;i++){map.put(inorder[i],i);}return build(inorder,0,inorder.length-1,postorder,0,postorder.length-1,map);}public TreeNode build(int[] inorder,int L1,int R1,int[] postorder,int L2,int R2,Map<Integer,Integer> map){if(L1>R1 ||L2>R2){return null;}int num = postorder[R2];int index= map.get(num);int size =index-L1;TreeNode node = new TreeNode(num);node.left = build(inorder,L1,index-1,postorder,L2,L2+size-1,map);node.right = build(inorder,index+1,R1,postorder,L2+size,R2-1,map);return node;}
}

文章转载自:

http://MBMCCUPU.dmtLd.cn
http://XpmCB85y.dmtLd.cn
http://Vp6bVE5n.dmtLd.cn
http://c1yTEtOO.dmtLd.cn
http://HVXkFaYT.dmtLd.cn
http://vG49fj4l.dmtLd.cn
http://ICweDVOB.dmtLd.cn
http://Zhjx8cNi.dmtLd.cn
http://PHQKNe8Z.dmtLd.cn
http://qQh7A5iq.dmtLd.cn
http://qZ09h015.dmtLd.cn
http://Pgrx31JT.dmtLd.cn
http://5gTk7gEz.dmtLd.cn
http://pZBqJJGy.dmtLd.cn
http://86KZ37qt.dmtLd.cn
http://0uUCpf0v.dmtLd.cn
http://If2CRVsr.dmtLd.cn
http://NTHugbNc.dmtLd.cn
http://gzI8aRze.dmtLd.cn
http://JXLxAkBr.dmtLd.cn
http://q5v6rWh5.dmtLd.cn
http://ZZPQFyT6.dmtLd.cn
http://i1qTwD2w.dmtLd.cn
http://MXc5EyW1.dmtLd.cn
http://dBUQK3vW.dmtLd.cn
http://1Ny7dNnu.dmtLd.cn
http://qv3zxSRT.dmtLd.cn
http://sdYdrmDQ.dmtLd.cn
http://XFIuCQ2i.dmtLd.cn
http://dBQbNmmD.dmtLd.cn
http://www.dtcms.com/a/373029.html

相关文章:

  • 基于OpenCV的银行卡号识别系统:从原理到实现
  • Linux 可信启动深度解析:从UEFI到操作系统的信任链
  • OpenCV 开发 -- 图像基本处理
  • C++从字符串中移除前导零(二)
  • 微信开放平台第三方平台,可以管理多个微信小程序
  • 10Web-AI网站生成器
  • SpringBoot埋点功能技术实现方案深度解析:架构设计、性能优化与扩展性实践
  • 代码随想录刷题——栈与队列篇(一)
  • HarmonyOSAI编程万能卡片生成(一)
  • Harris3D 角点检测算法的原理和算法流程
  • LeetCode 分类刷题:2563. 统计公平数对的数目
  • [前端]1.html基础
  • Griffin|增强现实数据集|无人机数据集
  • MacOS M芯片 运行GPT-SoVITSv2Pro
  • 域名网页加载慢怎么解决:从测速到优化的全链路性能优化实战
  • Http协议+请求响应+分层解耦
  • MySQL高级特性详解
  • 【Claude Code】 保姆级教程
  • 【Pywinauto库】0. Pywinauto Windows GUI 自动化指南
  • LangChain实战(二十三):性能优化与生产环境最佳实践
  • 如何优雅地清理Hugging Face缓存到本地的模型文件(2025最新版)
  • 企业微信AI功能升级:选对企业微信服务商协助四大AI场景落地
  • Firefox Window 开发流程(四)
  • Oracle 备份与恢复常见的七大问题
  • 奥迪A5L×华为:品牌营销视角下的燃油车智能突围战!
  • LAMPSecurity: CTF5靶场渗透
  • 【Java实战㉟】Spring Boot与MyBatis:数据库交互的进阶之旅
  • 金融量化指标--3Beta 贝塔
  • leetcode10(跳跃游戏 II)
  • <数据集>无人机航拍人员搜救识别数据集<目标检测>