当前位置:网站首页>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;
}
}
边栏推荐
- BSN long story 10: how to ensure the safety of NFT
- leetcode:111. 二叉树的最小深度
- 渗透常用工具-Goby
- Eat a rich woman's melon...
- “中移链”国密引擎在BSN正式上线
- CRC check
- CSDN一站式云服务开放内测,诚邀新老用户来抢鲜
- [laravel] detailed explanation of faker data filling
- Common penetration tools -goby
- Is the securities account opened by Yixue school for individuals safe? Is there a routine
猜你喜欢

Wechat emoticons are written into the judgment, and the OK and bomb you send may become "testimony in court"

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

Eat a rich woman's melon...

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

Apple amplification! It's done so well

A quietly rising domestic software, low-key and powerful!

Venv: directory structure of venv

这样理解mmap,挺有意思!

Module 9: design e-commerce seckill system

Who's still buying three squirrels
随机推荐
Apple amplification! It's done so well
【邂逅Django】——(二)数据库配置
Eat a rich woman's melon...
Common penetration tools -goby
关于#SQL#的问题,如何解决?
【黑马早报】俞敏洪称从来不看新东方股价;恒驰5将于7月开启预售;奈雪虚拟股票或涉嫌非法集资;7月1日起冰墩墩停产...
Today in history: the semiconductor war in the late 1990s; Von Neumann published the first draft; CBS acquires CNET
预制菜迎来“黄金时代”,谁能领跑下一个万亿市场
Raspberry pie 4B system construction (ultra detailed version)
Can MySQL CDC take out the op field
历史上的今天:九十年代末的半导体大战;冯·诺依曼发表第一份草案;CBS 收购 CNET...
4hutool实战:DateUtil-格式化时间[通俗易懂]
Ubuntu system installation and MySQL configuration
Po mode deep encapsulation
Can you afford to buy a house in Beijing, Shanghai, Guangzhou and Shenzhen with an annual salary of 1million?
Thread Basics
全球基金和资管的股票建仓率达到15年内新低
What should I learn in the zero foundation entry test? It's the most comprehensive. Just learn from it
Programmers want to go to state-owned enterprises? The technology is backward and the salary is low. I can't find a job after lying flat for several years
Is it safe to buy funds on the access letter?