当前位置:网站首页>JZ7 rebuild binary tree
JZ7 rebuild binary tree
2022-07-25 06:20:00 【Programmer Xiao Li】
The given number of nodes is n The results of preorder traversal and inorder traversal of binary tree , Please rebuild the binary tree and return its head node .
For example, input preamble traversal sequence {1,2,4,7,3,5,6,8} And middle order ergodic sequence {4,7,2,1,5,3,8,6}, The reconstruction is as shown in the figure below .

The order of preorder traversal is :root -> left -> right
The order of middle order traversal is : left -> root -> right

The first element of preorder traversal , It must be the root node . In the center traversal ,root The front must be the left subtree ,root The next one must be the right subtree .
1. In the preamble traversal , Find the root node , namely pre[0], Initialization preamble 、 Traverse the range of data in the array in middle order preStart, preEnd, inStart, inEnd

2. In the middle order traversal , Find the location of the root node ,rootIndexInVin

3. Then according to the principle just , We can separate pre and vin According to the length of the data 、 The position of the left subtree determines the new preStart, preEnd, inStart, inEnd

according to root The location of ,inStart, inEnd We can be sure ,inStart~rootIndexInVin - 1 It belongs to the left subtree , Can determine the left subtree inStart, inEnd

alike , It can also be based on inStart, inEnd We can be sure ,rootIndexInVin + 1 ~ inEnd Belongs to the right subtree , Can determine the right subtree inStart, inEnd

thus , We calculated , The length of the left subtree , The length of the right subtree :


4. According to the length of the left subtree and the right subtree , as well as preStart, preEnd Calculate the range of the left and right subtrees in the preorder traversal .

We know , The first element in the preorder traversal is the root node , thus , from preStart + 1 Start , continued len(L) Each element is a left subtree , therefore , We can confirm the scope
preStart' = preStart + 1
preEnd' = preStart' + len(L) - 1 = (preStart + 1) + leftNodeLength - 1

On the basis of the left subtree range , Further delay len(R) The first element is the right subtree range
preStart" = preEnd' + 1 = (preStart + 1 + leftNodeLength - 1) + 1
preEnd" = preStart" + len(R) - 1 = (preStart + 1 + leftNodeLength - 1 + 1) + rightNodeLength - 1
Okay, so , Recursively complete the reconstruction of binary tree , Until all elements are traversed .
import java.util.*;
/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public TreeNode reConstructBinaryTree(int [] pre,int [] vin) {
return construct(pre, 0, pre.length - 1, vin, 0, vin.length - 1);
}
private TreeNode construct(int[] pre, int preStart, int preEnd, int[] vin, int inStart, int inEnd){
if (pre == null || pre.length == 0 || vin == null || vin.length == 0){
return null;
}
if (preStart > preEnd || inStart > inEnd){
return null;
}
// The first element of the preorder traversal is the current root node
int rootVal = pre[preStart];
// Find in the array traversed in middle order , Location of the root node
int rootIndexInVin = inStart;
while (rootIndexInVin <= inEnd && vin[rootIndexInVin] != rootVal){
rootIndexInVin++;
}
// The left subtree nodes of the root node have a total of :inStart~rootIndexInVin-1
int leftNodeLength = rootIndexInVin - inStart;
int rightNodeLength = inEnd - rootIndexInVin;
TreeNode rootNode = new TreeNode(rootVal);
rootNode.left = construct(pre, preStart + 1, (preStart + 1) + leftNodeLength - 1, vin, inStart, rootIndexInVin - 1);
rootNode.right = construct(pre, (preStart + 1 + leftNodeLength - 1) + 1, (preStart + 1 + leftNodeLength - 1) + 1 + rightNodeLength - 1, vin, rootIndexInVin + 1, inEnd);
return rootNode;
}
}边栏推荐
- [reprint] pycharm packages.Py program as executable exe
- 机器学习 Keras拟合正弦函数
- Review of some classic exercises of arrays
- Tutorial: encryption keys in cloud (using golang and CLI)
- (牛客多校二)J-Link with Arithmetic Progression(最小二乘法/三分)
- [unity3d] ugui callback function
- (2022年牛客多校一)I-Chiitoitsu(期望DP)
- The LAF protocol elephant of defi 2.0 may be one of the few profit-making means in your bear market
- (Niuke multi School II) j-link with arithmetic progress (least square method / three points)
- 阻塞队列分析
猜你喜欢

Unity Animator动画与状态机

What determines the "personality" of AI robots?
Shell script realizes the scheduled backup of MySQL database on two computers
![[ultra detailed diagram] FPN + mask RCNN](/img/ef/ddd62fe7e54074c134aa5ee4cc5840.png)
[ultra detailed diagram] FPN + mask RCNN

Evolution of coupon architecture under C2B mode

How to play a data mining game entry Edition

【Unity3D】UGUI回调函数
![[C language] in depth understanding of pointers and arrays (phase I)](/img/4b/26cf10baa29eeff08101dcbbb673a2.png)
[C language] in depth understanding of pointers and arrays (phase I)

node.express中req.body总是undefind解决
![(16)[系统调用]追踪系统调用(3环)](/img/b0/011351361135fd9f8e2d0d31749f73.png)
(16)[系统调用]追踪系统调用(3环)
随机推荐
ARM裸板调试之JTAG调试源码级调试
“蔚来杯“2022牛客暑期多校训练营2 Link with Game Glitch (spfa找正负环)
How does vscode enable multiple terminals? How to display horizontally?
Jstat command summary [easy to understand]
Shell script realizes the scheduled backup of MySQL database on two computers
Learning notes: detailed use of 12864 LCD module
(2022 Niuke multi School II) k-link with bracket sequence I (dynamic planning)
Case ---- how efficient is the buffer stream compared with the ordinary input stream and output stream?
Tutorial: encryption keys in cloud (using golang and CLI)
VBA common objects
GF Securities online account opening? Is it safe?
(2022牛客多校二)L-Link with Level Editor I(动态规划)
What determines the "personality" of AI robots?
Xiaomi 12s UTRA Leica watermark generation online tool
Define usage method and template
(2022牛客多校二)K-Link with Bracket Sequence I(动态规划)
Pdf snapshot artifact
“font/woff“ and “font/woff2“ in file “mime.types“
Multithreading programming under Win32 API
【Jailhouse 文章】Base Architectures for virtual-physical computing(2018)