[LeetCode]Construct Binary Tree from Inorder and Postorder Traversal

智能音箱开发者必看:CEVA-BX vs Cadence HiFi音频DSP实战选型指南(附功耗对比表) 本文深入对比了CEVA-BX和Cadence HiFi两大音频DSP架构在智能音箱开发中的性能差异,涵盖语音唤醒、多麦克风阵列处理等核心场景。通过实测数据和功耗对比表,为开发者提供选型参考,帮助优化产品性能和能效表现。 阅读详情

题目描述

Given inorder and postorder traversal of a tree, construct the binary tree.

Note:
You may assume that duplicates do not exist in the tree.

已知一棵二叉树的中序和后续遍历,构造出一棵二叉树。假设二叉树的节点无重复。

解题思路

乍一看题,好似无从解决,但画一个二叉树,并写出它的中序遍历和后序遍历结果,即可发现规律。


上图中可以发现后序遍历的数组的末尾元素即为根节点,根据根节点数值可以确定中序遍历数组中根节点的位置pos,继而可以得到:
  1. 左子树的中序遍历数组长度为pos-1,元素为inorder[0]~inorder[pos-1];
  2. 左子树的后序遍历数组长度为pos-1,元素为postorder[0]~postorder[pos-1];
  3. 右子树的中序遍历数组长度为inorder.length-1-pos,元素为inorder[pos+1]~inorder[inorder.length-1];
  4. 右子树的中序遍历数组长度为inorder.length-1-pos,元素为postorder[pos]~postorder[postorder.length-2]。
通过规律,我们很容易得到本题的递归解法。
至于本题的非递归解法还在研究中,容以后补上;

代码

public TreeNode buildTree(int[] inorder, int[] postorder) {
        TreeNode root;
        int pos = 0;
        
        if(inorder==null || postorder==null){
        	return null;
        }
        if(inorder.length==0|| postorder.length==0){
        	return null;
        }
        
        root = new TreeNode(postorder[postorder.length - 1]);
        for(int i = 0;i < inorder.length;i++){
        	if(inorder[i]==postorder[postorder.length - 1]){
        		pos = i;
        		break;
        	}
        }
        
        int[] inorderLeftChild = Arrays.copyOfRange(inorder, 0, pos);
        int[] postorderLeftChild = Arrays.copyOfRange(postorder, 0, pos);
        
        int[] inorderRightChild = Arrays.copyOfRange(inorder, pos+1, inorder.length);
        int[] postorderRightChild = Arrays.copyOfRange(postorder, pos, postorder.length-1);
        
        TreeNode leftChild = buildTree(inorderLeftChild,postorderLeftChild);
        TreeNode rightChild = buildTree(inorderRightChild,postorderRightChild);
        
        root.left = leftChild;
        root.right = rightChild;
        return root;
	}


使用MATLAB实现PAC2002轮胎模型的TIR文件解析和模型计算详解 PAC2002轮胎模型是由荷兰TNO公司开发的一种半经验的轮胎模型,用于描述轮胎在不同工况下的力学特性。该模型基于一系列物理和经验参数,通过复杂的数学表达式来模拟轮胎的力学行为,包括纵向力、侧向力、回正力矩等。 阅读详情

相关推荐

BERTopic模型入门教程(附代码+参数调节)

BERTopic 是一种用于从文本数据中提取主题模型的新型方法。通过结合 BERT嵌入和传统的主题模型来生成语义丰富的主题。传统的主题模型(如LDA)主要依赖于词频矩阵,而 BERTopic 通过预训练语言模型生成高维语义嵌入来捕捉词语间的复杂关系,因此可以更好地处理上下文信息。BERTopic模型可以看作是创建其主题表示的一系列步骤。核心原理如下:就本质而言,BERTopic就是一个拼装框架,可以把每部分都替换成其他模型,只要完成任务即可。

m0_52069102的博客 1万+

【Leetcode】Construct Binary Tree from Inorder and Postorder Traversal

题目链接:https://leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/ 题目: Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may a

业精于勤,荒于嬉 3602

revit二次开发环境搭建(2024版)(小白友好)

revit二次开发环境配置,SDK、addin manager和lookup新手友好安装教程

m0_56961251的博客 1428

LeetCode Construct Binary Tree from Inorder and Postorder Traversal

Given inorder and postorder traversal of a tree, construct the binary tree.思路分析:这题主要考察递归,我们可以画一个二叉树的实例来具体分析。比如如果postorder 是84526731,inorder是84521637,首先我们可以知道postorder的最后一个数1必然是root,然后我们可以在inorder里面找到这

Coding for Dreams 2378

手把手教你用STM32+LAN8720搭建以太网通信模块(含原理图)

本文详细介绍了如何使用STM32微控制器与LAN8720物理层芯片搭建以太网通信模块,涵盖硬件设计、软件配置及常见问题排查。重点讲解了LAN8720的低功耗设计、RMII接口简化布线等特性,以及STM32CubeMX配置和LwIP协议栈移植技巧,助力开发者快速实现稳定可靠的嵌入式网络通信。

CAT789的专栏 427

车载测试实战:adb logcat日志高效抓取与问题定位技巧

本文详细介绍了车载测试中adb logcat日志的高效抓取与问题定位技巧。作为车载测试的“第一现场”,adb logcat能实时、精准地抓取系统与应用日志,是定位应用崩溃、ANR及功能异常等问题的关键工具。文章从环境搭建、核心命令组合拳到实战分析与自动化脚本,提供了完整的操作指南,帮助测试人员快速掌握这一必备技能,大幅提升问题排查效率。

ii567的博客 1053

[LeetCode] Construct Binary Tree from Inorder and Postorder Traversal

[Problem] Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. [Solution] class Solution {public: // find elem

1.02^365的成长裂变 3637

OSCP-1-3-natraj(本地文件包含、日志中毒、apache2配置漏洞、nmap提权)

像往常一样,我们将使用“Netdiscover”工具识别主机的 IP。所以,让我们首先用 nmap 列出所有 TCP 端口。我们从访问web服务(80端口)开始,在那里我们找到了几张关于Natraj的图片和信息,我们将查看源代码和robots.txt,似乎没有什么有用的。(或至少,目前)。因此,让我们继续进行。在 Dirb 及其默认字典的帮助下,我们找到了一个名为“”的目录。我们进入并列出一个名为“”的文件:如果我们执行它,我们会看到它什么也不做。我们可能需要添加其他内容。

墨痕诉清风的博客 537

Leetcode 106. Construct Binary Tree from Inorder and Postorder Traversal

文章作者:Tyan 博客:noahsnail.com &nbsp;|&nbsp; CSDN &nbsp;|&nbsp; 简书 1. Description 2. Solution /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *le...

SnailTyan 300

【swjtu】算法实验2.4分析算法时间复杂度

分析给定算法的时间复杂度。 上机实验,绘制不同输入情况下的运行时间曲线,并与(1)中的分析结果进行比较。 撰写相应的实验报告,实验报告内容包括:实验目的、实验任务、 实验环境、实验步骤、实验结果及其分析以及实验总结等部分内容。

qq_52046937的博客 738

Steam崩溃:ntdll.dll报错0xc0000374...如何解决?

🏆本文收录于 《全栈 Bug 调优(实战版)》 专栏。专栏聚焦真实项目中的各类疑难 Bug,从成因剖析 → 排查路径 → 解决方案 → 预防优化全链路拆解,形成一套可复用、可沉淀的实战知识体系。无论你是初入职场的开发者,还是负责复杂项目的资深工程师,都可以在这里构建一套属于自己的「问题诊断与性能调优」方法论,助你稳步进阶、放大技术价值。

**My Coding Family** 1187

LeetCode 106 Construct Binary Tree from Inorder and Postorder Traversal

这个是一道深度优先搜索的题目。 我们如何处理中序遍历和后序遍历的数组,使其能够表示成一颗二叉搜索树呢? 我是这样处理的。 首先,我们知道后序遍历的数组。从后到前是树根-子树根-子子树根,好了,有了这个递归的顺序我们就可以执行了。 首先取出树根节点,在中序遍历的数组中寻找这个节点所在的位置,那么这个位置的左侧就是左子树,右侧是右子树。 根据这个道理再递归求解就可以了。唯一需要注意的是开始和

input1991的专栏 516

强化学习:在智能交通系统中的应用

作者:禅与计算机程序设计艺术 这篇文章将探索如何利用强化学习(RL)这一强大的机器学习方法,在智能交通系统中实现自动驾驶、优化路线规划和提高道路安全等方面的突破。RL通过让智能体与环境互动并从这些互动中学习,从而解决复杂的决策过程问题,这正是现代智能交通系统所需要的。 背景介绍 随着城市化进程的加速,交通拥堵、能源消耗和交通事

AI天才研究院 1140

【LeetCode】Construct Binary Tree from Inorder and Postorder Traversal 解题报告

【题目】 Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. 【解析】 题意:根据二叉树中序遍历和后序遍历的结果,构造该二叉树。 首先明确一下,中序遍

跳出温水的青蛙 1374

C# WinForm 基于DirectShow.dll编写的程序,可边预览边保存视频,音频和图片

基于微软的DirectShow.dll的二次开发1.自动识别连接的摄像头跟麦克风且可以对其进行选择(默认选中第一个)2.可录制wmv和avi两种格式的视频并且都带有音频,avi可以录制的分辨率和帧数进行选择,wmv默认进行了淸晰度优化如不喜欢可去除代码中有相关提示3.可在预览点开之后进行拍照,也可边录制视频边拍照4.添加了视频和音频的压缩功能(酌情使用)这次二次开发发现C#对DirectShow的相关信息太少,这次做完也不敢藏私共享出来,还望大家指正

visio安装包

Microsoft Office Visio 是一款专业的制图软件。利用经过更新的形状、协作工具和数据链接图表,创建专业级图表以简化复杂的信息。

LeetCode_Construct Binary Tree from Inorder and Postorder Traversal

给定二叉树中序、后序遍历结果,构建二叉树。 后序的最后一个结点必为根节点。 对于后序的最后一个结点而言,中序遍历结果中,位于它左边的是它的左子树,位于它右边的是它的右子树。 由 2 可得基本递归思路一:不断利用后序最后一个节点将中序结果分割,自底向上递归建立左右子树即可。此时,由于要在中序结果中寻找后序最后结点,故要么每次都循环遍历(时间代价),要么提前建立一个哈希表(空间代价)作为参数。 本文的重点是第二种递归思路,既不循环也不需要哈希表。 代码如下:int pInorder; // index o

rotk2015的博客 291

廖雪峰Python教程[完整版]

廖雪峰的python教程整理的,已经排好版了,而且还是完整的,适合小白

[LeetCode 106] Construct Binary Tree from Inorder and Postorder Traversal

题目链接:construct-binary-tree-from-inorder-and-postorder-traversal import java.util.Arrays; //根据中序和后序遍历构建二叉树 /** * * Given Inorder and Postorder traversal of a tree, construct the binary tr

瓦尔登湖 730

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

LeetCode 106:Construct Binary Tree from Postorder and Inorder Traversal

Given inorder and postorder traversal of a tree, construct the binary tree. 给定一个二叉树的后序和中序遍历,重建这棵二叉树。 此题和LeetCode105 根据前序和中序重建二叉树类似。 所谓后序遍历,即先访问根的左、右子树,然后再访问根节点。这样根节点在二叉树后序遍历的最后一个个元素。 所谓中序遍

哈哈的个人专栏 981

leetcode之Construct Binary Tree from Inorder and Postorder Traversal 问题

问题描述: Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. 啰嗦一句,可能学过数据结构的人看到题目就知道啥意思了,给的问题介绍和描述几乎没啥意思。 示例:

漫漫学习路 743

【LeetCode】Construct Binary Tree from Inorder and Postorder Traversal

Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. /** * Definition for binary tree * struct TreeNode {

R_xiaozhu_Q的专栏 1035

【leetcode】Construct Binary Tree from Inorder and Postorder Traversal

Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. Accept: 22ms 思路: 中根遍历+后根遍历还原一棵二叉树。to

我的编程笔记 468

LeetCode: Construct Binary Tree from Inorder and Postorder Traversal

Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. /** * Definition for binary tree * struct TreeNode {

Tingmei的专栏 1548

LeetCode: Construct Binary Tree from Inorder and Postorder Traversal 解题报告

Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that duplicates do not exist in t...

weixin_34195546的博客 267

LeetCode106. Construct Binary Tree from Inorder and Postorder Traversal

根据给定的中序和后序排列,重建二叉树

grllery的博客 421

LeetCode[Tree]: Construct Binary Tree from Inorder and Postorder Traversal

Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. 这个题目的解法与LeetCode[Tree]: Construct Binary Tree from Pre

chfe910的专栏 810
上一篇: [LeetCode]Remove Element
下一篇: [LeetCode]Reorder List
彩虹之西
博客等级 码龄16年 18粉丝 133原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值