当前位置:网站首页>106. construct binary tree from middle order and post order traversal sequence
106. construct binary tree from middle order and post order traversal sequence
2022-07-01 10:19:00 【hequnwang10】
One 、 Title Description
Given two arrays of integers inorder and postorder , among inorder Is the middle order traversal of a binary tree , postorder Is the post order traversal of the same tree , Please construct and return this Binary tree .
Example 1:

Input :inorder = [9,3,15,20,7], postorder = [9,15,7,20,3]
Output :[3,9,20,null,null,15,7]
Example 2:
Input :inorder = [-1], postorder = [-1]
Output :[-1]
Two 、 Problem solving
Find four nodes
/** * 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) {
// This is similar to the construction of binary tree by preorder traversal and inorder traversal
// Mainly find four nodes Use hashMap Save the nodes traversed in the middle order
if(inorder == null || postorder == null || inorder.length == 0 || postorder.length == 0){
return null;
}
// Save the nodes traversed in the middle order
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){
// Judge array
if(ileft > iright || pleft > pright){
return null;
}
// Find the root node
int rootnum = postorder[pright];
// Create a root node
TreeNode root = new TreeNode(rootnum);
// Split array
int inorderindex = map.get(rootnum);
// The length of the left subtree
int leftlength = inorderindex - ileft;
// The next segment of the postorder traversal
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;
}
}
边栏推荐
- 线程基础知识
- This is the best flash popular science article I have ever seen!
- C [byte array] and [hexadecimal string] mutual conversion - codeplus series
- TC8:UDP_USER_INTERFACE_01-08
- 【邂逅Django】——(二)数据库配置
- SQLAchemy 常用操作
- tryhackme圣诞挑战2021-Advent of Cyber 3-day1-IDOR漏洞,不安全的访问控制漏洞
- About database: how to avoid deadlock in gbase 8s
- Does anyone know the logic of limit statement execution in Clickhouse? In the picture, the SQL above can be executed successfully
- CRC 校驗
猜你喜欢

预制菜迎来“黄金时代”,谁能领跑下一个万亿市场

Finally, someone made it clear what DRAM and NAND flash are

Introduction of uniapp wechat applet components on demand

直播管理项目

【邂逅Django】——(二)数据库配置

TC8:UDP_USER_INTERFACE_01-08

微信表情符号写入判决书,你发的OK、炸弹都可能成为“呈堂证供”

venv: venv 的目录结构
![C# [字节数组]与[16进制字符串]互相转换 - CodePlus系列](/img/d2/dad88f53701c7cd7638bd4983cbb4b.png)
C# [字节数组]与[16进制字符串]互相转换 - CodePlus系列

Zero foundation software testing must see, 10 years of testing old bird's conscience suggestions (a total of 15)
随机推荐
超标量处理器设计 姚永斌 第4章 分支预测 --4.1 小节摘录
Does anyone know why? The table structure is the source table MySQL CDC that has just been directly copied
C one line code calculates the MD5 value of the file - codeplus series
leetcode:111. Minimum depth of binary tree
BSN long story 10: how to ensure the safety of NFT
Tryhackme Christmas challenge 2021 advance of cyber 3-day1-idor vulnerability, insecure access control vulnerability
4hutool practice: dateutil format time [easy to understand]
Which securities company has a low, safe and reliable Commission for stock trading and account opening
The stock position building rate of global funds and asset management reached a new low in 15 years
日本教授起诉英特尔FPGA与SoC产品侵犯一项设计专利
好高的佣金,《新程序员》合伙人计划来袭,人人皆可参与!
BSN长话短说之十:如何保证NFT的安全
Wechat emoticons are written into the judgment, and the OK and bomb you send may become "testimony in court"
scratch大鱼吃小鱼 电子学会图形化编程scratch等级考试二级真题和答案解析2022年6月
Packetdrill script analysis guide
Venv: directory structure of venv
项目必用的全局异常处理器,你学会了吗
Floyd repeat
What should I learn in the zero foundation entry test? It's the most comprehensive. Just learn from it
.NET 5.0+ 无需依赖第三方 原生实现定时任务