当前位置:网站首页>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;
}
}
边栏推荐
- SSH服务器拒绝密码,再试一次;PermitRootLogin yes无效问题
- 谁还在买“三只松鼠”们
- 硬件中台项目
- Which securities company has a low, safe and reliable Commission for stock trading and account opening
- It is interesting to understand MMAP in this way!
- Flinkv1.13 implementation of financial anti fraud cases
- MySQL interception_ MySQL method for intercepting strings [easy to understand]
- Comparison between Oracle JDK and openjdk
- Swag init error: cannot find type definition: response Response
- Packetdrill script analysis guide
猜你喜欢

Common penetration tools -goby
![[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

The programmer was beaten.

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

The latest masterpiece of Alibaba, which took 182 days to produce 1015 pages of distributed full stack manual, is so delicious

Fried money, lost 10million.

谁还在买“三只松鼠”们

睡了二哥。。。

【黑马早报】俞敏洪称从来不看新东方股价;恒驰5将于7月开启预售;奈雪虚拟股票或涉嫌非法集资;7月1日起冰墩墩停产...

The stock position building rate of global funds and asset management reached a new low in 15 years
随机推荐
Can you afford to buy a house in Beijing, Shanghai, Guangzhou and Shenzhen with an annual salary of 1million?
[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
新数据库时代,不要只学 Oracle、MySQL
“中移链”国密引擎在BSN正式上线
Floyd repeat
云原生到底是什么?它会是未来发展的趋势吗?
PO模式深入封装
Ubuntu system installation and MySQL configuration
How do clients request databases?
IPv6 learning notes
Precautions for lvgl v8.2 string display on keil MDK (take little bear pie as an example)
4hutool practice: dateutil format time [easy to understand]
Continue to advance, and softcom power steadily promotes cloud intelligence strategy
Daily mathematics serial 55: February 24
STM32 inverter power supply design scheme, based on STM32F103 controller [easy to understand]
What should I learn in the zero foundation entry test? It's the most comprehensive. Just learn from it
IDEA运行报错Command line is too long. Shorten command line for...
京东与腾讯续签三年战略合作协议;起薪涨至26万元!韩国三星SK争相加薪留住半导体人才;Firefox 102 发布|极客头条...
Who has the vision to cross the cycle?
Meituan P4 carefully collated microservice system architecture design manual to see the world of microservice architecture