当前位置:网站首页>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;
}
}
边栏推荐
- 怎么理解JS Promise
- How to understand JS promise
- SQL statement modify field type "suggestions collection"
- 4hutool practice: dateutil format time [easy to understand]
- This is the best flash popular science article I have ever seen!
- Fried money, lost 10million.
- leetcode:111. Minimum depth of binary tree
- CodeBlocks 左侧项目栏消失,workspace 自动保存项目,Default workspace,打开上次的workspace,工作区(图文教程,已解决)
- 项目必用的全局异常处理器,你学会了吗
- Module 9: design e-commerce seckill system
猜你喜欢

IDEA运行报错Command line is too long. Shorten command line for...

What a high commission! The new programmer's partner plan is coming. Everyone can participate!

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

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

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

数字藏品新一轮热度开启

直播管理项目

CRC 校验

Who's still buying three squirrels

uniapp微信小程序组件按需引入
随机推荐
Kotlin coprocessor scheduling switch threads it's time to unravel the truth
The latest masterpiece of Alibaba, which took 182 days to produce 1015 pages of distributed full stack manual, is so delicious
请问有没有人知道clickhouse 中 limit语句执行的逻辑,图片中,上面的SQL可以执行成功
有大佬知道这是为啥吗?表结构都是刚直接复制的源表 mysql-cdc
超标量处理器设计 姚永斌 第4章 分支预测 --4.1 小节摘录
谁还在买“三只松鼠”们
数字藏品市场新局面
Button button clear border
Packetdrill script analysis guide
Prefabricated dishes usher in the "golden age", who can lead the next trillion market
【论文阅读】Trajectory-guided Control Prediction for End-to-end Autonomous Driving: A Simple yet Strong Ba
4hutool practice: dateutil format time [easy to understand]
SQL server2014 failed to delete the database, with an error offset of 0x0000
Is it safe to do fund fixed investment on CICC securities?
Tryhackme Christmas challenge 2021 advance of cyber 3-day1-idor vulnerability, insecure access control vulnerability
Error: missing revert data in call exception
CSDN一站式云服务开放内测,诚邀新老用户来抢鲜
Venv: directory structure of venv
直播管理项目
关于OpenCV中图像的widthStep