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

边栏推荐
- The difference between ID selector and class selector
- How to retrieve the root password of MySQL if you forget it
- 深度學習 卷積神經網絡(CNN)基礎
- Some problems encountered in cocos2d-x project summary
- 什么是pyc文件
- MySql的root密码忘记该怎么找回
- 解决Thinkphp框架应用目录下数据库配置信息修改后依然按默认方式连接
- 字节跳动Dev Better技术沙龙成功举办,携手华泰分享Web研发效能提升经验
- gst-launch的-v参数
- Leetcode brush question: binary tree 14 (sum of left leaves)
猜你喜欢

Force buckle 1200 Minimum absolute difference

95后阿里P7晒出工资单:狠补了这个,真香...

14. Users, groups, and permissions (14)

How to apply smart contracts more wisely in 2022?

【无标题】

Zero cloud new UI design

再忙不能忘安全

C application interface development foundation - form control (5) - grouping control

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

aggregate
随机推荐
C application interface development foundation - form control (6) - menu bar, toolbar and status bar controls
[OBS] qstring's UTF-8 Chinese conversion to blog printing UTF-8 char*
[hard core dry goods] which company is better in data analysis? Choose pandas or SQL
sun.misc.BASE64Encoder报错解决方法[通俗易懂]
What are general items
.Net分布式事务及落地解决方案
C application interface development foundation - form control (5) - grouping control
Common operators and operator priority
Flume series: interceptor filtering data
微信小程序正则表达式提取链接
随机数生成的四种方法|Random|Math|ThreadLocalRandom|SecurityRandom
建立自己的网站(16)
leetcode刷题:二叉树12(二叉树的所有路径)
Float.floatToRawIntBits的返回值具体意思,将float转为byte数组
Zero cloud new UI design
Debezium series: idea integrates lexical and grammatical analysis ANTLR, and check the DDL, DML and other statements supported by debezium
使用 RepositoryProvider简化父子组件的传值
1:引文;
MySql的root密码忘记该怎么找回
Force buckle 729 My schedule I