LeetCode Construct Binary Tree from Inorder and Postorder Traversal

Construct Binary Tree from Preorder and Inorder Traversal Construct Binary Tree from Preorder and Inorder Traversal 根据先序,中序建立二叉树 立即下载

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

思路分析:这题主要考察递归,我们可以画一个二叉树的实例来具体分析。比如如果postorder 是84526731,inorder是84521637,首先我们可以知道postorder的最后一个数1必然是root,然后我们可以在inorder里面找到这个root 1,进而把inorder划分成了左半部分8452和右半部分637,显然8452和637就是左右子树的inorder序列。同时我们还可以求出左右子树中node的个数,在这个例子中左子树有4个node,右子树有3个node,然后我们可以利用这个信息去postorder里面去找到左右子树的postorder序列,分别是8452和673,于是我们就知道了左子树和右子树的inorder及postorder,划归为了规模小1的原问题,于是就可以递归实现了。实现中用到了一个hashmap来保存inorder从value到index的映射,可以在o(1)的时间内从val得到index。注意这题假设树中所有val没有重复,如果有重复要在inorder中找到哪个是root就困难了。遇到关于tree的题目往往和递归有关,可以通过画一个实例来找规律,找出f(n)和f(n-1)的关系,一般可以从f(n-1)出发推算出f(n),就可以递归实现。

AC Code

/**
 * Definition for binary tree
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public TreeNode buildTree(int[] inorder, int[] postorder) {
        HashMap<Integer, Integer> inorderValueToIndexMap = new HashMap<Integer, Integer>();
        for(int i = 0; i < inorder.length; i++){
            inorderValueToIndexMap.put(inorder[i], i);
        }
        return buildTreeHelper(postorder, 0, postorder.length - 1, inorder, 0, inorder.length - 1, inorderValueToIndexMap);
    }
    
    public TreeNode buildTreeHelper(int [] postorder, int ps, int pe, int[] inorder, int is, int ie, HashMap<Integer, Integer> inorderValueToIndexMap){
        if(ps > pe || is > ie) return null;
        TreeNode root = new TreeNode(postorder[pe]);
        int indexOfRootInorder = inorderValueToIndexMap.get(root.val);
        int leftNum = indexOfRootInorder - is;
        int rightNum = ie - indexOfRootInorder;
        root.left = buildTreeHelper(postorder, ps, ps + leftNum - 1, inorder, indexOfRootInorder - leftNum, indexOfRootInorder -1, inorderValueToIndexMap);
        root.right = buildTreeHelper(postorder, ps + leftNum, ps + leftNum + rightNum - 1, inorder, indexOfRootInorder + 1, indexOfRootInorder + rightNum, inorderValueToIndexMap);
        return root;
    }
}


ctf——writeup ctf线上赛——第三关地址:http://106.75.86.18:2222提示:ip spoofing——ip伪造首先访问目标地址,一个正常的网页,挺文艺的。上面有些提示。根据上面的提示,我们浏览网页。找了好久,终于找到你。这个跳转链接。(在这个页面下方)我们接着点击这个链接,就跳转到administrator.php这个页面了。看到这个登录页面,就要知道用户名,密码,可是没有怎么办。一开始也试 阅读详情

相关推荐

KBS投稿全流程拆解:从模板准备到条件接收的实战指南

本文详细拆解了KBS期刊投稿的全流程,从模板准备、投稿系统操作到返修策略和条件接收。重点介绍了Cover Letter撰写技巧、Latex模板选择、审稿周期管理以及返修文件的系统准备方法,帮助研究者高效完成KBS投稿并提升接收概率。

weixin_26938645的博客 713

【LeetCode 面试经典150题】106. Construct Binary Tree from Inorder and Postorder Traversal 中后序遍历构造二叉树

Given two integer arrays and where is the inorder traversal of a binary tree and is the postorder traversal of the same tree, construct and return the binary tree.给定两个整数数组 和 ,其中 是二叉树的中序遍历,而 是同一棵树的后序遍历,根据这两个数组构造并返回这棵二叉树。输入: = [9,3,15,20,7], = [9,15

qq_43513394的博客 506

Ardupilot环境搭建

Ardpilot介绍:ArduPilot通过多种传感器的数据(GPS、加速度计、气压计、磁场计)等来估计飞行器的姿态,从而使飞行器能够保持稳定。

weixin_62387234的博客 2643

Construct Binary Tree from Preorder and Postorder Traversal 根据前序和后序遍历构造二叉树

返回与给定的前序和后序遍历匹配的任何二叉树。  pre 和 post 遍历中的值是不同的正整数。 示例: 输入:pre = [1,2,4,5,3,6,7], post = [4,5,2,6,7,3,1] 输出:[1,2,3,4,5,6,7] 提示: 1 &lt;= pre.length == post.length &lt;= 30 pre[] 和 post[] 都是 1, 2, ...

qq_26410101的博客 310

从3D相机数据到三维模型:Halcon实战深度图、亮度图与点云转换全流程

本文详细解析了如何利用Halcon将3D相机采集的深度图、亮度图转换为三维点云模型的完整流程。通过实战案例分享,涵盖数据格式处理、点云生成方法(SDK接口与纯Halcon实现)、三维模型可视化优化等关键技术,帮助工程师解决工业检测中的实际难题,提升三维重建精度与效率。

weixin_42525005的博客 403

Elasticsearch连接中断:深入解析Connection reset by peer问题及优化策略

本文深入解析Elasticsearch连接中断问题,特别是Connection reset by peer错误的成因与解决方案。从TCP层的心跳机制到客户端与服务端的配置差异,详细介绍了网络不稳定、防火墙拦截等常见原因,并提供了六种优化策略,包括KeepAlive调整、TCP参数调优和智能重试机制等,帮助提升Elasticsearch连接的稳定性。

weixin_42546392的博客 415

LeetCode OJ 之 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. 给定一棵树的前序和后序遍历结果,构造这棵二叉树,假设二叉树不存在重复的元素。 思路:

灯火阑珊处 603

axi时序图_(AXI)握手协议(pvld/prdy或者valid-ready)中ready打拍技巧

内容提要ready打拍的问题用FIFO的思路去解决用Buffer的思路去解决问题提出:ready时序如何优化?在valid/ready 握手协议中,valid 与 data的时序优化比较容易理解,(不熟悉valid/ready协议或者valid打拍方法的)大家可以参考上次推送(握手协议(pvld/prdy或者valid-ready或AXI)中Valid及data打拍技巧)。 但是有时候,关键路径是...

weixin_29763641的博客 3334

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.For example, giveninorder = [9,3,15,20,7] postorder = [9,15,7,20...

zrh_CSDN的博客 343

Win10 IPv6 远程桌面连接(小米路由器)

背景 由于家里是移动网,且懒得向运营商要公网IPv4,所以想着用IPv6试一下。 家里的环境是这样的: 宽带用的是移动的,没有公网IPv4,但经过测试发现IPv6是公网的 上网通过光猫拨号,而不是通过路由器 路由器是小米AC2100 光猫更改为桥接模式 路由器必须要通过PPPoE拨号,如果是光猫拨号,那么要找对应的维护人员辅助你把光猫改成桥接模式。通过PPPoE拨号路由器就能够拿到公网的IPv6网段,也就可以给底下的其他服务分配公网IPv6了。而如果是光猫拨号,那么只有路由器可以拿到公网的IP。其他的设

timczm的博客 1万+

CentOS7 内核升级

可在http://ftp.sjtu.edu.cn/sites/elrepo.org/linux/kernel/el7/x86_64/RPMS选择对应架构平台不受影响的内核版本文件下载,然后离线安装,也可通过yum下载包后进行安装。通过 #uname -sr ,可以看到当前的内核版本 Linux 3.10.0-1160.el7.x86_64。首先找台能访问互联网的机器,通过联网的机器进行内核包下载。lt:longterm的缩写:长期维护版;ml:mainline的缩写:最新稳定版;

Jian Sun_的博客 604

LeetCode 106-Construct Binary Tree from Inorder and Postorder Traversal(二叉树,后序+中序序列构造二叉树)

LeetCode 106-Construct Binary Tree from Inorder and Postorder Traversal题解

祝如愿的博客 216

使用curl命令出现fatal: unable to access ‘https://github.com/nvm-sh/nvm.git/‘: Encountered end of file的解决方案

本文主要介绍了使用curl命令出现fatal: unable to access ‘https://github.com/nvm-sh/nvm.git/’: Encountered end of file的解决方案,希望能对使用curl命令的同学们有所帮助。 文章目录 1. 问题描述 2. 解决方案

weixin_43178406的博客 1万+

[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

WINCC7.3破解文件

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

全球国家地区城市数据

MySQL全球国家地区城市数据报。。。。。。。。。。。。

【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

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

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

LeetCode_Construct Binary Tree from Inorder and Postorder Traversal

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

rotk2015的博客 290

LeetCode 106 Construct Binary Tree from Inorder and Postorder Traversal

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

input1991的专栏 516

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 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

瓦尔登湖 729

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 {

Tingmei的专栏 1548

【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

我的编程笔记 467

[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 the

xshalk的专栏 2880

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
上一篇: LeetCode Merge Sorted Array
下一篇: LeetCode Construct Binary Tree from Preorder and Inorder Traversal
LarryNLPIR
LarryNLPIR 领域专家: 算法与数据结构技术领域 领域专家: 算法与数据结构技术领域
博客等级 码龄18年 2853粉丝 243原创
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值