当前位置:网站首页>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});
}
}
边栏推荐
- 挖财钱堂教育靠谱安全吗?
- Android interview classic, 2022 Android interview written examination summary
- 【obs】libobs-winrt :CreateDispatcherQueueController
- A solution to PHP's inability to convert strings into JSON
- leetcode刷题:二叉树17(从中序与后序遍历序列构造二叉树)
- Using repositoryprovider to simplify the value passing of parent-child components
- Add data to excel small and medium-sized cases through poi
- 浅浅的谈一下ThreadLocalInsecureRandom
- Leetcode: binary tree 15 (find the value in the lower left corner of the tree)
- ICTCLAS用的字Lucene4.9捆绑
猜你喜欢
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
No matter how busy you are, you can't forget safety
淺淺的談一下ThreadLocalInsecureRandom
Leetcode brush question: binary tree 14 (sum of left leaves)
Wechat applet regular expression extraction link
The city chain technology Digital Innovation Strategy Summit was successfully held
Inventory of the most complete low code / no code platforms in the whole network: Jiandao cloud, partner cloud, Mingdao cloud, Qingliu, xurong cloud, Jijian cloud, treelab, nailing · Yida, Tencent clo
微信小程序正则表达式提取链接
Add data to excel small and medium-sized cases through poi
leetcode刷题:二叉树18(最大二叉树)
随机推荐
Float. The specific meaning of the return value of floattorawintbits is to convert float into byte array
Two pits exported using easyexcel template (map empty data columns are disordered and nested objects are not supported)
深度學習 卷積神經網絡(CNN)基礎
Jvmrandom cannot set seeds | problem tracing | source code tracing
Analysis of openh264 decoded data flow
Is it safe for Galaxy Securities to open an account online?
How to apply smart contracts more wisely in 2022?
ffplay文档[通俗易懂]
处理文件和目录名
【c语言】归并排序
Debezium series: parsing the default value character set
Force buckle 729 My schedule I
Complete interview questions for interviewers and senior Android engineers in front-line Internet enterprises
No matter how busy you are, you can't forget safety
Tasks in GStreamer
sun.misc.BASE64Encoder报错解决方法[通俗易懂]
The difference between ID selector and class selector
js实现禁止网页缩放(Ctrl+鼠标、+、-缩放有效亲测)
【无标题】
Go language | 01 wsl+vscode environment construction pit avoidance Guide