当前位置:网站首页>Leetcode skimming: binary tree 17 (construct binary tree from middle order and post order traversal sequence)
Leetcode skimming: binary tree 17 (construct binary tree from middle order and post order traversal sequence)
2022-07-05 20:03:00 【Taotao can't learn English】
106. Construct binary tree from middle order and post order traversal sequence
According to the middle order traversal and post order traversal of a tree, a binary tree is constructed .
Be careful :
You can assume that there are no duplicate elements in the tree .
for example , give
In the sequence traversal inorder = [9,3,15,20,7]
After the sequence traversal postorder = [9,15,7,20,3]
Return to the following binary tree :

The difficulty is good , Recursive method . The termination condition is The length of both arrays is 0 when , return null . After the sequence traversal Left Right root . Every time the post order traversal is constructed , The last of the subsequent array is always the root node , Then according to this root node , There are also subject regulations No repeating elements , Then find the current root node in the ordered array , Take it as the boundary to divide the middle order array into the middle order array of the left subtree and the middle order array of the right subtree . And in the middle order array Root node The location of , Just put it in the subsequent array , This position is forward yes Postorder traversal of left subtree , This position includes the position backwards until The next to last element Is the postorder traversal of the right subtree , The last one is the root node .
Then take the first of the left subtree 、 Post array and First of right subtree 、 The last array can be recursive .
package com.programmercarl.tree;
/** * @ClassName BuildTree * @Descriotion TODO * @Author nitaotao * @Date 2022/7/5 12:37 * @Version 1.0 * https://leetcode.cn/problems/construct-binary-tree-from-inorder-and-postorder-traversal/ * 106. Construct binary tree from middle order and post order traversal sequence **/
public class BuildTree {
/** * @param inorder In the sequence traversal 1 2 * @param postorder After the sequence traversal 2 1 * @return */
public TreeNode buildTree(int[] inorder, int[] postorder) {
if (inorder.length == 0 && postorder.length == 0) {
return null;
}
// The length of the last array is equal , Because the number of elements is the same
// The first element of post order traversal is the root node
TreeNode root = new TreeNode(postorder[postorder.length - 1]);
// Find the root node in the middle order traversal
int rootIndex = 0;
for (int i = 0; i < inorder.length; i++) {
if (root.val == inorder[i]) {
rootIndex = i;
}
}
// The medium order array is divided into left subtree and right subtree
int[] leftIn = new int[rootIndex];
int[] rightIn = new int[inorder.length - rootIndex - 1];
for (int i = 0; i < inorder.length; i++) {
if (i < rootIndex) {
leftIn[i] = inorder[i];
} else if (i > rootIndex) {
rightIn[i-rootIndex-1] = inorder[i];
}
}
// After order traversal array front rootIndex Bit is left subtree , after rootIndex Bit is a right subtree
int[] leftPost = new int[rootIndex];
int[] rightPost = new int[inorder.length - rootIndex - 1];
// The last node , It has been built as the root node .
for (int i = 0; i < postorder.length - 1; i++) {
if (i < rootIndex) {
leftPost[i] = postorder[i];
} else {
rightPost[i - rootIndex] = postorder[i];
}
}
// Construct left and right subtrees
root.left = buildTree(leftIn, leftPost);
root.right = buildTree(rightIn, rightPost);
return root;
}
public static void main(String[] args) {
new BuildTree().buildTree(new int[]{
1, 2}, new int[]{
2, 1});
}
}

边栏推荐
- How to select the Block Editor? Impression notes verse, notation, flowus
- What is the core value of testing?
- Leetcode brush question: binary tree 14 (sum of left leaves)
- 通配符选择器
- c——顺序结构
- c语言oj得pe,ACM入门之OJ~
- 【无标题】
- Common operators and operator priority
- JVMRandom不可设置种子|问题追溯|源码追溯
- Using repositoryprovider to simplify the value passing of parent-child components
猜你喜欢

webuploader文件上传 拖拽上传 进度监听 类型控制 上传结果监听控件
![[C language] string function and Simulation Implementation strlen & strcpy & strcat & StrCmp](/img/32/738df44b6005fd84b4a9037464e61e.jpg)
[C language] string function and Simulation Implementation strlen & strcpy & strcat & StrCmp

Go language | 02 for loop and the use of common functions

How to select the Block Editor? Impression notes verse, notation, flowus

2023年深圳市绿色低碳产业扶持计划申报指南

如何安全快速地从 Centos迁移到openEuler

Using repositoryprovider to simplify the value passing of parent-child components

Autumn byte interviewer asked you any questions? In fact, you have stepped on thunder

After 95, Alibaba P7 published the payroll: it's really fragrant to make up this

Hong Kong stocks will welcome the "best ten yuan store". Can famous creative products break through through the IPO?
随机推荐
港股将迎“最牛十元店“,名创优品能借IPO突围?
挖财钱堂教育靠谱安全吗?
Debezium series: record the messages parsed by debezium and the solutions after the MariaDB database deletes multiple temporary tables
【c语言】快速排序的三种实现以及优化细节
Relationship between floating elements and parent and brother boxes
Leetcode brush questions: binary tree 11 (balanced binary tree)
Leetcode skimming: binary tree 10 (number of nodes of a complete binary tree)
No matter how busy you are, you can't forget safety
618 "low key" curtain call, how can baiqiushangmei join hands with the brand to cross the "uncertain era"?
DP: tree DP
What is the function of okcc call center
Force buckle 729 My schedule I
.Net分布式事务及落地解决方案
The city chain technology Digital Innovation Strategy Summit was successfully held
How to select the Block Editor? Impression notes verse, notation, flowus
股票开户哪里好?网上客户经理开户安全吗
[FAQ] summary of common causes and solutions of Huawei account service error 907135701
Based on vs2017 and cmake GUI configuration, zxing and opencv are used in win10 x64 environment, and simple detection of data matrix code is realized
Zhongang Mining: analysis of the current market supply situation of the global fluorite industry in 2022
Database logic processing function