当前位置:网站首页>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;
}
}
边栏推荐
- SQL server2014 failed to delete the database, with an error offset of 0x0000
- IDEA运行报错Command line is too long. Shorten command line for...
- 关于#SQL#的问题,如何解决?
- I like two men...
- Prefabricated dishes usher in the "golden age", who can lead the next trillion market
- 关于#数据库#的问题:GBase 8s中如何避免死锁
- 12. Gateway new generation gateway
- Daily mathematics serial 55: February 24
- 数字藏品新一轮热度开启
- 在中金证券上做基金定投安全吗?
猜你喜欢
Scratch big fish eat small fish Electronic Society graphical programming scratch grade examination level 2 true questions and answers analysis June 2022
程序员都想去国企?技术落后薪资低,躺平几年出来都找不到工作...
新数据库时代,不要只学 Oracle、MySQL
零基础入行软件测试必看,10年测试老鸟的良心建议(共15条)
JD and Tencent renewed the three-year strategic cooperation agreement; The starting salary rose to 260000 yuan! Samsung sk of South Korea competes for salary increase to retain semiconductor talents;
【Matytype】在CSDN博客中插入Mathtype行间与行内公式
CCNP Part XII BGP (IV)
日本教授起诉英特尔FPGA与SoC产品侵犯一项设计专利
What a high commission! The new programmer's partner plan is coming. Everyone can participate!
北汽蓝谷:业绩承压,极狐难期
随机推荐
Common penetration tools -goby
About widthstep of images in opencv
High precision factorial
大佬们 有没有搞过sink分流写入clickhouse 或者其他数据库的操作。
Ssh server rejects password, try again; Permitrootlogin yes invalid problem
What a high commission! The new programmer's partner plan is coming. Everyone can participate!
北汽蓝谷:业绩承压,极狐难期
投稿开奖丨轻量应用服务器征文活动(5月)奖励公布
Po mode deep encapsulation
CCNP Part XII BGP (IV)
Wechat emoticons are written into the judgment, and the OK and bomb you send may become "testimony in court"
MySQL常用命令
Centos 配置discuz 提示请检查 mysql 模块是否正确加载
大佬们,数据湖iceberg的数据,怎样导出到mysql? 有什么工具? sqoop,datax都没
What should I learn in the zero foundation entry test? It's the most comprehensive. Just learn from it
tryhackme圣诞挑战2021-Advent of Cyber 3-day1-IDOR漏洞,不安全的访问控制漏洞
Meituan P4 carefully collated microservice system architecture design manual to see the world of microservice architecture
Tearful eyes, it's not easy to change jobs. Three rounds of interviews, four hours of soul torture
Eat a rich woman's melon...
Error: missing revert data in call exception