当前位置:网站首页>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});
}
}
边栏推荐
- 字节跳动Dev Better技术沙龙成功举办,携手华泰分享Web研发效能提升经验
- 图嵌入Graph embedding学习笔记
- leetcode刷题:二叉树18(最大二叉树)
- Is it safe for Galaxy Securities to open an account online?
- DP:树DP
- leetcode刷题:二叉树14(左叶子之和)
- leetcode刷题:二叉树12(二叉树的所有路径)
- 银河证券在网上开户安全吗?
- S7-200smart uses V90 Modbus communication control library to control the specific methods and steps of V90 servo
- Zero cloud new UI design
猜你喜欢
秋招字节面试官问你还有什么问题?其实你已经踩雷了
Leetcode brush question: binary tree 14 (sum of left leaves)
JS implementation prohibits web page zooming (ctrl+ mouse, +, - zooming effective pro test)
leetcode刷题:二叉树17(从中序与后序遍历序列构造二叉树)
Let's talk about threadlocalinsecurerandom
Force buckle 729 My schedule I
.Net分布式事務及落地解决方案
Recommended collection, my Tencent Android interview experience sharing
Jvmrandom cannot set seeds | problem tracing | source code tracing
Go language | 03 array, pointer, slice usage
随机推荐
【c语言】快速排序的三种实现以及优化细节
Do you know several assertion methods commonly used by JMeter?
[FAQ] summary of common causes and solutions of Huawei account service error 907135701
Build your own website (16)
Parler de threadlocal insecurerandom
[C language] merge sort
Database logic processing function
DP: tree DP
线程池参数及合理设置
多分支结构
Jvmrandom cannot set seeds | problem tracing | source code tracing
C langue OJ obtenir PE, ACM démarrer OJ
Ffplay document [easy to understand]
Go language learning tutorial (16)
[C language] string function and Simulation Implementation strlen & strcpy & strcat & StrCmp
js实现禁止网页缩放(Ctrl+鼠标、+、-缩放有效亲测)
Solve the problem that the database configuration information under the ThinkPHP framework application directory is still connected by default after modification
aggregate
How to retrieve the root password of MySQL if you forget it
1: Citation;