当前位置:网站首页>Sword finger offer 07 Rebuild binary tree
Sword finger offer 07 Rebuild binary tree
2022-07-05 04:17:00 【xzystart】
The finger of the sword Offer 07. Reconstruction of binary tree
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 .
Example 1:
Input: preorder = [3,9,20,15,7], inorder = [9,3,15,20,7]
Output: [3,9,20,null,null,15,7]
Example 2:
Input: preorder = [-1], inorder = [-1]
Output: [-1]
Limit :
0 <= Number of nodes <= 5000
class Solution {
int[] preorder; // Preorder traversal array
HashMap<Integer, Integer> map = new HashMap<>(); // Middle order traversal table mapping The key traverses the value of the array in the preceding order , It's worth it inorder Position in array
public TreeNode buildTree(int[] preorder, int[] inorder) {
// According to the previous traversal, we can get that the root node is the first element of the array
// According to the root node, you can get it into the array , The left of the root node is the left subtree , On the right of the root node is the right subtree
this.preorder = preorder;
for(int i = 0 ;i<preorder.length;i++){
// Save mapping
map.put(inorder[i],i);
}
return helper(0,inorder.length-1,0);
}
private TreeNode helper(int left,int right,int root){
// among root by preorder Position of the first element in the array ,
// The left and right pointers point to inorder The left and right bounds of the array
// Exit conditions ( When the left and right pointers point to the same position , It proves that he is a leaf node , There are no nodes below )
if(left>right) return null;
// Create a header node
TreeNode head = new TreeNode(preorder[root]);
// Get the subscript of the corresponding element of the end node in the ordered array
int n = map.get(preorder[root]);
// The index of the root of the left subtree is the root node in the preorder +1
// The left boundary of the recursive left subtree is the original middle order in_left
// The right boundary of the recursive left subtree is the index of the root node in the middle order -1
head.left = helper(left,n-1,root+1);
// The index of the root of the right subtree is The current root position + The number of left subtrees + 1
// The left boundary of the recursive right subtree is the current root node in the middle order +1
// The right boundary of the recursive right subtree is the boundary of the original right subtree in the middle order
head.right = helper(n+1,right,root+1+(n-left));
head.right = helper(n+1,right,root+1+(n-left));
// n - left + root + 1 Means Root node index + Left subtree length + 1
//Input: preorder = [3,9,20,15,7], Here, the root is 3 when , The length of the left subtree is 1, The calculation method is inorder = [9,3,15,20,7] in 3 Index subscript of - Left boundary
return head;
}
}
边栏推荐
- C language course setting: cinema ticket selling management system
- [moteur illusoire UE] il ne faut que six étapes pour réaliser le déploiement du flux de pixels ue5 et éviter les détours! (4.26 et 4.27 principes similaires)
- Rust区块琏开发——签名加密与私钥公钥
- On the day 25K joined Tencent, I cried
- lds链接的 顺序问题
- Machine learning -- neural network
- 【UNIAPP】系统热更新实现思路
- Moco is not suitable for target detection? MsrA proposes object level comparative learning target detection pre training method SOCO! Performance SOTA! (NeurIPS 2021)...
- mysql的七种join连接查询
- 10种寻址方式之间的区别
猜你喜欢
Kwai, Tiktok, video number, battle content payment
【虚幻引擎UE】实现测绘三脚架展开动画制作
Three level linkage demo of uniapp uview u-picker components
Rome链分析
How to solve the problem that easycvr changes the recording storage path and does not generate recording files?
Wechat applet development process (with mind map)
[understand series after reading] 6000 words teach you to realize interface automation from 0 to 1
Interview related high-frequency algorithm test site 3
3. Package the bottom navigation tabbar
Containerd series - detailed explanation of plugins
随机推荐
Judge whether the stack order is reasonable according to the stack order
Threejs Internet of things, 3D visualization of farms (II)
PR video clip (project packaging)
Study notes 7
lds链接的 顺序问题
WGS84 coordinate system, web Mercator, gcj02 coordinate system, bd09 coordinate system - brief introduction to common coordinate systems
JVM garbage collection
Number of possible stack order types of stack order with length n
Threejs implements labels and displays labels with custom styles
C language course setting: cinema ticket selling management system
The scale of computing power in China ranks second in the world: computing is leaping forward in Intelligent Computing
机器学习 --- 神经网络
【虛幻引擎UE】實現UE5像素流部署僅需六步操作少走彎路!(4.26和4.27原理類似)
Three level linkage demo of uniapp uview u-picker components
EasyCVR平台出现WebRTC协议视频播放不了是什么原因?
【thingsboard】替换首页logo的方法
Why can't all browsers on my computer open web pages
A application wakes up B should be a fast method
[uniapp] system hot update implementation ideas
[thingsboard] how to replace the homepage logo