当前位置:网站首页>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;
}
}
边栏推荐
- 投稿开奖丨轻量应用服务器征文活动(5月)奖励公布
- Ubuntu system installation and MySQL configuration
- Prefabricated dishes usher in the "golden age", who can lead the next trillion market
- SQL SERVER2014删除数据库失败,报错偏移量0x0000...
- [laravel] detailed explanation of faker data filling
- CSDN一站式云服务开放内测,诚邀新老用户来抢鲜
- SQL optimization - in and not in, exist
- MySQL interception_ MySQL method for intercepting strings [easy to understand]
- Kotlin 协程调度切换线程是时候解开真相了
- 哪个券商公司炒股开户佣金低又安全又可靠
猜你喜欢

【Matytype】在CSDN博客中插入Mathtype行间与行内公式

TC8:UDP_USER_INTERFACE_01-08

建议收藏 | 在openGauss上遇到慢SQL该怎么办?

CSDN一站式云服务开放内测,诚邀新老用户来抢鲜

Packetdrill script analysis guide

uniapp微信小程序组件按需引入

What is cloud primordial? Will it be the trend of future development?

缺少比较器,运放来救场!(运放当做比较器电路记录)

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

机器学习之线性回归详解
随机推荐
A quietly rising domestic software, low-key and powerful!
SQL statement modify field type "suggestions collection"
CentOS configures discuz prompt, please check whether the MySQL module is loaded correctly
关于OpenCV中图像的widthStep
Button button clear border
建议收藏 | 在openGauss上遇到慢SQL该怎么办?
Thread Basics
If you meet a female driver and drive didi as an amateur, you can earn 500 a day!
北汽蓝谷:业绩承压,极狐难期
The programmer was beaten.
大佬们,数据湖iceberg的数据,怎样导出到mysql? 有什么工具? sqoop,datax都没
Apple amplification! It's done so well
MySQL常用命令
Packetdrill script analysis guide
STM32逆变器电源设计方案,基于STM32F103控制器[通俗易懂]
This is the best flash popular science article I have ever seen!
uniapp微信小程序组件按需引入
SQL SERVER2014删除数据库失败,报错偏移量0x0000...
TC8:UDP_ USER_ INTERFACE_ 01-08
Sqlization is the first step in ETL incremental production. What are the core capabilities of such an architecture?