当前位置:网站首页>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 化是 ETL 增量生产的第一步,这样的架构的核心能力是什么?
- PO模式深入封装
- SQL statement modify field type "suggestions collection"
- Ubuntu system installation and MySQL configuration
- 推荐一款 JSON 可视化工具神器!
- tryhackme圣诞挑战2021-Advent of Cyber 3-day1-IDOR漏洞,不安全的访问控制漏洞
- CRC 校验
- MySQL常用命令
- 12.Gateway新一代网关
- 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;
猜你喜欢

CRC check

Module 9: design e-commerce seckill system

零基础入行软件测试必看,10年测试老鸟的良心建议(共15条)

零基础入门测试该学什么?最全整理,照着学就对了

硬件中台项目

TC8:UDP_USER_INTERFACE_01-08

Zero foundation software testing must see, 10 years of testing old bird's conscience suggestions (a total of 15)

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

Have you learned the necessary global exception handler for the project

CCNP Part XII BGP (IV)
随机推荐
历史上的今天:九十年代末的半导体大战;冯·诺依曼发表第一份草案;CBS 收购 CNET...
预制菜迎来“黄金时代”,谁能领跑下一个万亿市场
推荐一款 JSON 可视化工具神器!
BSN长话短说之十:如何保证NFT的安全
What should I learn in the zero foundation entry test? It's the most comprehensive. Just learn from it
项目必用的全局异常处理器,你学会了吗
106. 从中序与后序遍历序列构造二叉树
编写自己的who命令
Kotlin coprocessor scheduling switch threads it's time to unravel the truth
Error: missing revert data in call exception
Change password of MySQL version 5.7 and 8.0
STM32逆变器电源设计方案,基于STM32F103控制器[通俗易懂]
Packetdrill script analysis guide
venv: venv 的目录结构
SQL server2014 failed to delete the database, with an error offset of 0x0000
Kotlin 协程调度切换线程是时候解开真相了
php 实现抽奖功能
C# 一行代码计算文件的MD5值 - CodePlus系列
sql语句修改字段类型「建议收藏」
MySQL interception_ MySQL method for intercepting strings [easy to understand]