【leetcode】106(Medium)Construct Binary Tree from Inorder and Postorder Traversal

京东云鼎环境配置与API安全接入实战:ISV服务商如何搞定数据加密与商家授权 本文详细介绍了京东云鼎环境配置与API安全接入的实战指南,帮助ISV服务商解决数据加密与商家授权等核心问题。通过京东开放平台的应用申请流程,结合多租户数据隔离策略和自动化授权实现,确保电商生态中的安全高效运营。文章还提供了避坑指南,助力开发者规避常见配置错误。 阅读详情

解题思路:
递归
后序遍历的最后一个结点是根结点。根据这个根节点可以在中序遍历中找到左子树和右子树的结点集合,然后递归找到左子树和右子树的根节点。


提交代码:

class Solution {
    public TreeNode buildTree(int[] inorder, int[] postorder) {
        if(inorder.length==0)	return null;
        if(inorder.length==1) {
        	TreeNode node=new TreeNode(inorder[0]);
        	return node;
        }
        
        TreeNode root=new TreeNode(postorder[postorder.length-1]);
        int p=0,size=inorder.length;
        while(inorder[p]!=root.val)
        	p++;
        int[] leftInOrder=new int[p];
        int[] leftPostOrder=new int[p];
        for(int i=0;i<p;i++) {
        	leftInOrder[i]=inorder[i];
        	leftPostOrder[i]=postorder[i];
        }
        root.left=buildTree(leftInOrder,leftPostOrder);
        
        int[] rightInOrder=new int[size-p-1];
        int[] rightPostOrder=new int[size-p-1];
        for(int i=p+1,j=0;i<size;i++,j++) 
        	rightInOrder[j]=inorder[i];
        for(int i=p,j=0;i<size-1;i++,j++)
        	rightPostOrder[j]=postorder[i];
        root.right=buildTree(rightInOrder,rightPostOrder);
        
        return root;
    }
 }

运行结果:
在这里插入图片描述

STM32F407ZGT6开发板跑马灯实战:从原理图到烧录全流程(附FlyMcu配置) 本文以正点原子F4探索者开发板(STM32F407ZGT6)为例,手把手指导完成跑马灯实验。内容涵盖从原理图解读、GPIO驱动逻辑分析、HAL库工程搭建,到代码编写与FlyMcu串口烧录配置的全流程,帮助嵌入式开发新手快速实现第一个LED控制程序,打通软硬件开发链路。 阅读详情

相关推荐

DDSP(Differentiable Digital Signal Processing) 开篇

文章目录DDSP可以用来做什么?下一步工作 工作中接触到了DDSP声码器,但非科班出声的我对这块知识非常陌生,甚至带有一些畏惧的心理,但是为了工作为了进步,必须硬下头皮去啃,相信只要耐心地坚持,没什么能难到我。于是从今天开始,就用这个博客专栏来记录我的学习历程,希望也能帮助到你~ DDSP对应的论文是:DDSP: Differentiable Digital Signal Processing 先翻译一篇来自DDSP论文作者的对DDSP的介绍的文章。 今天,我们很高兴地介绍可微数字信号处理(DDSP)库。

YeeKo的博客 9257

[Leetcode 106, Medium] Construct Binary Tree from Inorder and Postorder Traversal

[Leetcode 106, Medium] Construct Binary Tree from Inorder and Postorder Traversal

算法的实践 369

ADS2020实战:村田电感.s2p与.mod文件导入全流程对比(附仿真结果差异分析)

本文详细对比了ADS2020中村田电感的.s2p与.mod文件导入流程及仿真结果差异,重点分析了两种模型在低频段和高频段的精度表现。通过实际案例展示.s2p文件在高频仿真中的优势,以及.mod文件在低频和非线性特性模拟中的准确性,为射频电路设计提供模型选择策略和混合仿真技巧。

weixin_29289821的博客 526

LeetCode - Medium - 106. Construct Binary Tree from Inorder and Postorder Traversal

Topic Array Tree Depth-first Search Description https://leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/ Given two integer arrays inorder and postorder where inorder is the inorder traversal of a binary tree and postorder

KISS 276

基于rms包的限制性立方样条回归(RCS)R代码实现

RCS

2301_79584199的博客 2万+

SAP UI5 OData V4 操作结果访问:从 getObject 到返回值上下文与 $$inheritExpandSelect

SAP UI5 OData V4 操作结果获取指南 摘要:在 SAP UI5 OData V4 模型中,操作结果需通过绑定上下文获取而非直接从 invoke() 返回。本文详解核心要点: 数据获取路径 使用 invoke() 发起请求后,通过 getBoundContext().getObject() 获取结果 特定场景下 invoke() 可能返回返回值上下文(如草稿切换) 关键实践技巧 使用 $$inheritExpandSelect 继承字段选择,避免二次读取 对象页应用中,通过替换绑定上下文实现无缝

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 321

Leetcode 106. Construct Binary Tree from Inorder and Postorder Traversal (Medium) (cpp)

Leetcode 106. Construct Binary Tree from Inorder and Postorder Traversal (Medium) (cpp)

还是需要多学习!闷声大发财,这是坠吼的! 357

高速信号设计中的关键测试项目与测试仪器详解

高速信号设计是一个多维度协同优化的系统工程。时域分析如同外科手术刀般精准定位物理缺陷,频域分析提供传输通道的频谱指纹,信号质量评估量化系统鲁棒性,接收端容限测试验证极端工况下的可靠性,协议一致性确保生态互操作性,而电源完整性则直指诸多信号问题的根源。

u014344410的博客 221

【LeetCode】106.Construct Binary Tree from Inorder and Postorder Traversal(Medium)解题报告

【LeetCode】106.Construct Binary Tree from Inorder and Postorder Traversal(Medium)解题报告题目地址:https://leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/description/ 题目描述:

郝春雨的博客 538

仿真学习系列(三十一):Sigrity软件界面菜单栏学习(一)

Sigrity 的功能非常强大,根据不同的仿真对象与使用场景,被拆分为多个独立的专业组件(如 PowerSI、PowerDC、SystemSI、OptimizePI 等)。虽然各组件的界面布局略有不同,但整体菜单结构和操作逻辑是一致的。 为了降低入门门槛,本篇文章以 PowerSI 组件为例,带大家认识 Sigrity 软件中的核心菜单栏,并学习导入设计后常用的菜单功能。

案例文章,高速信号 662

【腾讯小龙虾 WorkBuddy 专栏 03】技能(Skills)制作全教程!自定义技能编写、导出分享、导入使用一步到位

🔥腾讯AI智能体WorkBuddy自定义技能全攻略 本文为零基础用户提供WorkBuddy自定义技能的完整教程,涵盖技能概念、编写规范到实操全流程。核心内容包括: 技能本质:标准化任务指令模板,可固化重复操作流程 三大价值:一键执行重复任务/跨设备共享/降低使用门槛 实操演示:以"桌面文档自动分类"为例,分步讲解编写、测试、导出 共享方案:支持单技能/批量导出,通过任意渠道传输分享 导入指南:3秒完成他人技能加载,并提供常见问题解决方案 高阶优化:增加异常处理、版本管理等提升技能普适性

AI人人懂 3081

[leetcode] 106. Construct Binary Tree from Inorder and Postorder Traversal(medium)

原题地址 思路: 和leetcode105题差不多,这道题是给中序和后序,求出二叉树。 解法一: 思路和105题差不多,只是pos是从后往前遍历,生成树顺序也是先右后左。 class Solution { public: TreeNode *buildTree(vector<int> &inorder, vecto...

Rh's blog 98

SpringBoot如何正确连接SqlServer(亲测可用)

对于SpringBoot连接SqlServer的方式和常见的错误的汇总。1、驱动程序无法通过使用安全套接字层(SSL)加密与 SQL Server 建立安全连接。2、com.microsoft.sqlserver.jdbc.SQLServerException: 对象名 ‘DUAL‘ 无效。

黄团团的个人博客 2万+

LeetCode-106:Construct Binary Tree from Inorder and Postorder Traversal (利用中序和后序遍历构建二叉树) -- medium

给定一棵树的中序和后序遍历数组,构建该二叉树

大树先生的博客 805

老版QQ原生表情gif动态

这是老版的QQ表情,不过全是动态的的,Windows照片查看器是不会动的,可以右击用浏览器打开查看,再说一遍是老版的,只是分享给宁愿用老版也要动态的人。

STM32F407中文手册

STM32f407中文手册 本参考手册面向应用开发人员,提供有关使用 STM32F405xx/07xx、STM32F415xx/17xx、 STM32F42xxx 和 STM32F43xxx 微控制器存储器与外设的完整信息。STM32F405xx/07xx、STM32F415xx/17xx、STM32F42xxx 和 STM32F43xxx 构成一个微 控制器系列,各产品具有不同的存储器大小、封装和外设。有关订购信息以及器件的机械与电气特性,请参见数据手册。有关 ARM Cortex™-M4F 内核的信息,请参见《Cortex™-M4F 技术参考手册》。

[LeetCode] 106. Construct Binary Tree from Postorder and Inorder Traversal_Medium tag: Tree Traversa...

Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that duplicates do not exist in the tree. For example, given inorder =[9,3,15,20,7] postorder = [9,...

weixin_30414635的博客 77

Probability and Statistics - 4th edition

Probability and Statistics - 4th edition

106.leetcode.Construct Binary Tree from Inorder and Postorder Traversal(medium)[中序、后序构造二叉树]

Given inorder and postorder traversal of a tree, construct the binary tree. 对于一棵二叉树可以通过先序、中序求得这棵树的结构,也可以通过后序、中序求得这棵树的结构,下面就是采用中序、后序构造的方法,对于后序可以确定每一步的根节点,再用中序可以确定左右子树。 /** * Definition for a binary

qz530308783的专栏 389

lwip-2.0.3

lwip是瑞典计算机科学院(SICS)的Adam Dunkels 开发的一个小型开源的TCP/IP协议栈。实现的重点是在保持TCP协议主要功能的基础上减少对RAM 的占用。

LeetCode: 106_Construct Binary Tree from Inorder and Postorder Traversal | 根据中序和后序遍历构建二叉树 | Medium...

要求:根据中序和后序遍历序列构建一棵二叉树 代码如下: 1 struct TreeNode { 2 int val; 3 TreeNode *left; 4 TreeNode *right; 5 TreeNode(int x): val(x),left(NULL), right(NULL) {} 6 }...

weixin_34014277的博客 92

Leetcode 106. Construct Binary Tree from Inorder and Postorder Traversal(详细解析)

Leetcode 106. Construct Binary Tree from Inorder and Postorder Traversal 题目链接: Construct Binary Tree from Inorder and Postorder Traversal 难度:Medium 题目大意: 根据一棵二叉树中序遍历和后续遍历的结果恢复出二叉树。 思路: 参考高赞回答。postorder的最后一个元素就是二叉树的根节点,找到根节点在inorder中的位置,inorder中根节点左边的所有元素就

xue1236的博客 370

leetcode -- 106. Construct Binary Tree from Inorder and Postorder Traversal

题目描述 题目难度:Medium Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. AC代码 /** * Definition for a binary tree no...

tkzc_csk的博客 171

LeetCode 106. Construct Binary Tree from Inorder and Postorder Traversal(二叉树)

题目来源:https://leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/ 问题描述 106.Construct Binary Tree from Inorder and Postorder Traversal Medium Given inorder and postord...

da_kao_la的博客 135

【LeetCode】 106. Construct Binary Tree from Inorder and Postorder Traversal 从中序与后序遍历序列构造二叉树(JAVA)

【LeetCode】 106. Construct Binary Tree from Inorder and Postorder Traversal 从中序与后序遍历序列构造二叉树(Medium)(JAVA) 题目地址: https://leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/...

qq_16927853的博客 347

[LeetCode] Construct Binary Tree from Inorder and Postorder Traversal

链接:https://leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/description/ 难度:Medium 题目:106. Construct Binary Tree from Inorder and Postorder Travers...

尾尾部落 114

LeetCode106 Construct Binary Tree from Inorder and Postorder Traversal

Given inorder and postorder traversal of a tree, construct the binary tree. (Medium) Note:You may assume that duplicates do not exist in the tree. 分析: 跟前一题方法一样,只需要分析出中序与后序恢复时,需从后序的最后一个元素读出来当做根节点...

weixin_30414245的博客 105

leetcode 106.construct-binary-tree-from-inorder-and-postorder-traversal 从中序与后序遍历序列构造二叉树 python3

时间:2020-8-18 题目地址:https://leetcode-cn.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/ 题目难度:Medium 题目描述: 根据一棵树的中序遍历与后序遍历构造二叉树。 注意: 你可以假设树中没有重复的元素。 例如,给出 中序遍历 inorder =[9,3,15,20,7] 后序遍历 postorder = [9,15,7,20,3] 返回如下的二叉树: ..

isabloomingtree的博客 236
上一篇: 【leetcode】105.(Medium)Construct Binary Tree from Preorder and Inorder Traveral
下一篇: 【leetcode】107.(Easy)Binary Tree Level Order Traversal II
AXIMI
博客等级 码龄10年 53粉丝 455原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值