当前位置:网站首页>Sword finger offer question brushing record - offer 07 Rebuild binary tree
Sword finger offer question brushing record - offer 07 Rebuild binary tree
2022-07-19 07:09:00 【When to order】
Enter the results of preorder traversal and inorder traversal of a binary tree , Build the binary tree and return its root node .
It is assumed that the results of the input preorder traversal and the middle order traversal do not contain repeated numbers .

The preorder traversal order of binary tree is :
Let's go through the root node ;
Then recursively traverse the left subtree ;
Finally, recursively traverse the right subtree .
The order of traversal in binary tree is :
First recursively traverse the left subtree ;
Then traverse the root node ;
Finally, recursively traverse the right subtree .
stay 「 recursive 」 In the process of traversing a subtree , We also regard this subtree as a brand new tree , Traverse in the above order . mining 「 The former sequence traversal 」 and 「 In the sequence traversal 」 The nature of , We can get the solution of this problem .
Ideas
For any tree , The form of preorder traversal is always
[ The root node , [ The result of preorder traversal of left subtree ], [ The result of preorder traversal of the right subtree ] ]
That is, the root node is always the first node in the preorder traversal . And the form of middle order traversal is always
[ [ Middle order traversal results of left subtree ], The root node , [ The middle order traversal result of the right subtree ] ]
As long as we locate the root node in the middle order traversal , Then we can know the number of nodes in the left subtree and the right subtree respectively . Because the length of preorder traversal and medial traversal of the same subtree is obviously the same , So we can correspond to the result of preorder traversal , Position all left and right brackets in the above form .
Since then , We know the results of preorder traversal and medial traversal of left subtree , And the results of preorder traversal and medial traversal of the right subtree , We can construct left subtree and right subtree recursively , Then connect the two subtrees to the left and right positions of the root node .
details
When the root node is located in the middle order traversal , A simple method is to scan the whole result of the middle order traversal directly and find the root node , But it's time-consuming . We can consider using a hash table to help us quickly locate the root node . For each key value pair in the hash map , A key represents an element ( The value of the node ), Value indicates where it appears in the middle order traversal . Before the process of constructing a binary tree , We can scan the list of traversal in the middle order again , You can construct this hash map . In the process of constructing the binary tree , We just need O(1)O(1) Time to locate the root node .
The following code gives detailed comments .
class Solution {
private Map<Integer, Integer> indexMap;
public TreeNode myBuildTree(int[] preorder, int[] inorder, int preorder_left, int preorder_right, int inorder_left, int inorder_right) {
if (preorder_left > preorder_right) {
return null;
}
// The first node in preorder traversal is the root node
int preorder_root = preorder_left;
// Locate the root node in the middle order traversal
int inorder_root = indexMap.get(preorder[preorder_root]);
// First set up the root node
TreeNode root = new TreeNode(preorder[preorder_root]);
// Get the number of nodes in the left subtree
int size_left_subtree = inorder_root - inorder_left;
// The left subtree is constructed recursively , And connect to the root node
// In the first order traversal 「 from Left boundary +1 At the beginning size_left_subtree」 Elements correspond to the middle order traversal 「 from Left boundary Start to Root node localization -1」 The elements of
root.left = myBuildTree(preorder, inorder, preorder_left + 1, preorder_left + size_left_subtree, inorder_left, inorder_root - 1);
// Construct the right subtree recursively , And connect to the root node
// In the first order traversal 「 from Left boundary +1+ The number of nodes in the left subtree Start to Right border 」 The element corresponding to the middle order traversal 「 from Root node localization +1 To Right border 」 The elements of
root.right = myBuildTree(preorder, inorder, preorder_left + size_left_subtree + 1, preorder_right, inorder_root + 1, inorder_right);
return root;
}
public TreeNode buildTree(int[] preorder, int[] inorder) {
int n = preorder.length;
// Construct a hash map , Help us quickly locate the root node
indexMap = new HashMap<Integer, Integer>();
for (int i = 0; i < n; i++) {
indexMap.put(inorder[i], i);
}
return myBuildTree(preorder, inorder, 0, n - 1, 0, n - 1);
}
}
边栏推荐
- [automated testing] - robotframework practice (III) writing test cases
- m基于matlab的协作mimo分布式空时编码技术的仿真
- CDN是什么?使用CDN有什么优势?
- How does legend open its service? What do you need to prepare to open legend private server?
- [untitled]
- urllib库的使用
- Xiaodi - network security notes (1)
- Regular expression, generator, iterator
- Utilisation et différenciation des dictionnaires, des tuples et des listes,
- M BTS antenna design based on MATLAB, with GUI interface
猜你喜欢

Filter过滤器

论文阅读:Deep Residual Learning in Spiking Neural Networks

我的世界 1.18.1 Forge版 开服教程,可装MOD,带面板

M simulation of DQPSK modulation and demodulation technology based on MATLAB

爬虫基础—代理的基本原理

m基于matlab的DQPSK调制解调技术的仿真

What role does 5g era server play in this?

Minecraft Paper 1.18.1 版开服教程,我的世界开服教程,MCSManager9面板使用教程

M FPGA implementation of chaotic digital secure communication system based on Lorenz chaotic self synchronization, Verilog programming implementation, with MATLAB chaotic program
Xiaodi network security notes - Information Collection - architecture, construction, WAF (8)
随机推荐
What does ack attack mean? How to defend ack attack
Xiaodi network security notes - Information Collection - architecture, construction, WAF (8)
Poor Xiaofan (simulation)
Matlab simulation of cognitive femtocell performance in m3gpp LTE communication network
Xiaodi network security - note encryption coding algorithm (6)
How do you know whether the network needs to use advanced anti DDoS server? How to choose the computer room is also very important, as well as the stability of the later business
网站被劫持了怎么办?
Xiaodi network security - Notes (5)
How to set primary key self growth in PostgreSQL database
m基于matlab的超宽带MIMO雷达对目标的检测仿真,考虑时间反转
Tower of Hanoi 2 (function)
Hermit crab and anemone
1.服务器是什么?
Homework
How does legend open its service? What do you need to prepare to open legend private server?
快速学会cut命令,uniq命令的使用
5G时代服务器在这里面起着什么作用?
Recursive access to directories, print Fibonacci sequences, high-order functions
Minecraft bedrock BDS service tutorial
Minecraft integration package [gtnh] gray Technology: new vision server building tutorial