当前位置:网站首页>106. 从中序与后序遍历序列构造二叉树
106. 从中序与后序遍历序列构造二叉树
2022-07-01 10:08:00 【hequnwang10】
一、题目描述
给定两个整数数组 inorder 和 postorder ,其中 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]
二、解题
找四个节点
/** * 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 {
Map<Integer,Integer> map = new HashMap<>();
public TreeNode buildTree(int[] inorder, int[] postorder) {
//这与前序遍历和中序遍历构造二叉树相似
//主要找四个节点 使用hashMap保存中序遍历的节点
if(inorder == null || postorder == null || inorder.length == 0 || postorder.length == 0){
return null;
}
//保存中序遍历的节点
for(int i = 0;i<inorder.length;i++){
map.put(inorder[i],i);
}
return myBuildTree(inorder,postorder,0,inorder.length-1,0,postorder.length-1);
}
public TreeNode myBuildTree(int[] inorder, int[] postorder,int ileft,int iright,int pleft,int pright){
//判断数组
if(ileft > iright || pleft > pright){
return null;
}
//找到根节点
int rootnum = postorder[pright];
//创建根节点
TreeNode root = new TreeNode(rootnum);
//分割数组
int inorderindex = map.get(rootnum);
//左子树的长度
int leftlength = inorderindex - ileft;
//后序遍历的下一个分段
root.left = myBuildTree(inorder,postorder,ileft,inorderindex-1,pleft,pleft+leftlength-1);
root.right = myBuildTree(inorder,postorder,inorderindex+1,iright,pleft+leftlength,pright-1);
return root;
}
}
边栏推荐
- 程序员都想去国企?技术落后薪资低,躺平几年出来都找不到工作...
- 哪个券商公司炒股开户佣金低又安全又可靠
- In terms of use
- TC8:UDP_ USER_ INTERFACE_ 01-08
- Eat a rich woman's melon...
- button按钮清除边框
- CodeBlocks 左侧项目栏消失,workspace 自动保存项目,Default workspace,打开上次的workspace,工作区(图文教程,已解决)
- I like two men...
- The "China Mobile Chain" state secret engine was officially launched on BSN
- 7-Zip 遭抵制?呼吁者定下“三宗罪”:伪开源、不安全、作者来自俄罗斯!
猜你喜欢

Cortex M4 systick details

The programmer was beaten.
![[dark horse morning post] Yu Minhong said he never looked at the stock price of New Oriental; Hengchi 5 will start pre-sale in July; Naixue virtual stock or suspected of illegal fund-raising; From Jul](/img/58/8d5c78d919ed60bc833ec4daa22e23.jpg)
[dark horse morning post] Yu Minhong said he never looked at the stock price of New Oriental; Hengchi 5 will start pre-sale in July; Naixue virtual stock or suspected of illegal fund-raising; From Jul

Precautions for lvgl v8.2 string display on keil MDK (take little bear pie as an example)

苹果放大招!这件事干的太漂亮了……

SQL SERVER2014删除数据库失败,报错偏移量0x0000...

7-Zip 遭抵制?呼吁者定下“三宗罪”:伪开源、不安全、作者来自俄罗斯!

Fried money, lost 10million.

Common penetration tools -goby

Kotlin 协程调度切换线程是时候解开真相了
随机推荐
Change password of MySQL version 5.7 and 8.0
日本教授起诉英特尔FPGA与SoC产品侵犯一项设计专利
Ubuntu system installation and MySQL configuration
What a high commission! The new programmer's partner plan is coming. Everyone can participate!
leetcode:111. Minimum depth of binary tree
有大佬知道这是为啥吗?表结构都是刚直接复制的源表 mysql-cdc
京东与腾讯续签三年战略合作协议;起薪涨至26万元!韩国三星SK争相加薪留住半导体人才;Firefox 102 发布|极客头条...
预制菜迎来“黄金时代”,谁能领跑下一个万亿市场
零基础入门测试该学什么?最全整理,照着学就对了
PHP string to binary conversion
年薪100万,在北上广深买的起房子吗?
mysql cdc能把能把op字段拿出来吗
12.Gateway新一代网关
Have you learned the necessary global exception handler for the project
Is the securities account opened by Yixue school for individuals safe? Is there a routine
直播管理项目
The latest masterpiece of Alibaba, which took 182 days to produce 1015 pages of distributed full stack manual, is so delicious
持续进阶,软通动力稳步推动云智能战略
BSN长话短说之十:如何保证NFT的安全
Daily mathematics serial 55: February 24