当前位置:网站首页>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});
}
}

边栏推荐
- third-party dynamic library (libcudnn.so) that Paddle depends on is not configured correctl
- Concept and syntax of function
- 函数的概念及语法
- c——顺序结构
- What is PyC file
- JVMRandom不可设置种子|问题追溯|源码追溯
- leetcode刷题:二叉树11(平衡二叉树)
- Two pits exported using easyexcel template (map empty data columns are disordered and nested objects are not supported)
- leetcode刷题:二叉树15(找树左下角的值)
- Using repositoryprovider to simplify the value passing of parent-child components
猜你喜欢

The city chain technology Digital Innovation Strategy Summit was successfully held

How about testing outsourcing companies?

40000 word Wenshuo operator new & operator delete

leetcode刷题:二叉树18(最大二叉树)
![[OBS] qstring's UTF-8 Chinese conversion to blog printing UTF-8 char*](/img/cc/172684664a9115943d45b0646ef110.png)
[OBS] qstring's UTF-8 Chinese conversion to blog printing UTF-8 char*

Debezium series: record the messages parsed by debezium and the solutions after the MariaDB database deletes multiple temporary tables

leetcode刷题:二叉树17(从中序与后序遍历序列构造二叉树)

Postman core function analysis - parameterization and test report

解决Thinkphp框架应用目录下数据库配置信息修改后依然按默认方式连接

Bitcoinwin (BCW) was invited to attend Hanoi traders fair 2022
随机推荐
The city chain technology Digital Innovation Strategy Summit was successfully held
Webuploader file upload drag upload progress monitoring type control upload result monitoring control
线程池参数及合理设置
third-party dynamic library (libcudnn.so) that Paddle depends on is not configured correctl
浅浅的谈一下ThreadLocalInsecureRandom
Autumn byte interviewer asked you any questions? In fact, you have stepped on thunder
手机股票开户安全吗?靠不靠谱啊?
Debezium series: idea integrates lexical and grammatical analysis ANTLR, and check the DDL, DML and other statements supported by debezium
港股将迎“最牛十元店“,名创优品能借IPO突围?
Leetcode: binary tree 15 (find the value in the lower left corner of the tree)
third-party dynamic library (libcudnn.so) that Paddle depends on is not configured correctl
No matter how busy you are, you can't forget safety
Build your own website (16)
ICTCLAS用的字Lucene4.9捆绑
Bzoj 3747 poi2015 kinoman segment tree
leetcode刷题:二叉树18(最大二叉树)
The difference between ID selector and class selector
Redis cluster simulated message queue
《乔布斯传》英文原著重点词汇笔记(十二)【 chapter ten & eleven】
股票开户哪里好?网上客户经理开户安全吗