当前位置:网站首页>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;
}
}
边栏推荐
- 新数据库时代,不要只学 Oracle、MySQL
- Daily mathematics serial 55: February 24
- Is the securities account opened by Yixue school for individuals safe? Is there a routine
- 7-Zip boycotted? The callers have committed "three crimes": pseudo open source, unsafe, and the author is from Russia!
- Scratch big fish eat small fish Electronic Society graphical programming scratch grade examination level 2 true questions and answers analysis June 2022
- Japanese professor sues Intel FPGA and SOC products for infringing a design patent
- Is it safe to buy funds on the access letter?
- Can you afford to buy a house in Beijing, Shanghai, Guangzhou and Shenzhen with an annual salary of 1million?
- 睡了二哥。。。
- leetcode:111. Minimum depth of binary tree
猜你喜欢

It is interesting to understand MMAP in this way!

HMS core audio editing service 3D audio technology helps create an immersive auditory feast

Swag init error: cannot find type definition: response Response

The stock position building rate of global funds and asset management reached a new low in 15 years

Introduction of uniapp wechat applet components on demand

7-Zip boycotted? The callers have committed "three crimes": pseudo open source, unsafe, and the author is from Russia!

Apple amplification! It's done so well

Flinkv1.13 implementation of financial anti fraud cases

Japanese professor sues Intel FPGA and SOC products for infringing a design patent

历史上的今天:九十年代末的半导体大战;冯·诺依曼发表第一份草案;CBS 收购 CNET...
随机推荐
树莓派4B系统搭建(超详细版)
Tearful eyes, it's not easy to change jobs. Three rounds of interviews, four hours of soul torture
Packetdrill script analysis guide
STM32 inverter power supply design scheme, based on STM32F103 controller [easy to understand]
How to understand JS promise
在中金证券上做基金定投安全吗?
Network partition notes
数据中台咋就从“小甜甜”变成了“牛夫人”?
Who's still buying three squirrels
sql语句修改字段类型「建议收藏」
日本教授起诉英特尔FPGA与SoC产品侵犯一项设计专利
Configure load balancing
HMS core audio editing service 3D audio technology helps create an immersive auditory feast
A quietly rising domestic software, low-key and powerful!
项目必用的全局异常处理器,你学会了吗
Huawei accounts work together at multiple ends to create a better internet life
线程基础知识
4hutool实战:DateUtil-格式化时间[通俗易懂]
全球基金和资管的股票建仓率达到15年内新低
It is interesting to understand MMAP in this way!