当前位置:网站首页>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;
}
}
边栏推荐
- 【虛幻引擎UE】實現UE5像素流部署僅需六步操作少走彎路!(4.26和4.27原理類似)
- 快手、抖音、视频号交战内容付费
- Wechat applet development process (with mind map)
- What is the reason why the webrtc protocol video cannot be played on the easycvr platform?
- 北京程序员的真实一天!!!!!
- How to realize real-time audio and video chat function
- 【FineBI】使用FineBI制作自定义地图过程
- C language course setting: cinema ticket selling management system
- User behavior collection platform
- How does the applet solve the rendering layer network layer error?
猜你喜欢
Network layer - forwarding (IP, ARP, DCHP, ICMP, network layer addressing, network address translation)
【虚幻引擎UE】运行和启动的区别,常见问题分析
【虚幻引擎UE】打包报错出现!FindPin错误的解决办法
快手、抖音、视频号交战内容付费
Longyuan war "epidemic" 2021 network security competition web easyjaba
How to solve the problem that easycvr changes the recording storage path and does not generate recording files?
About the recent experience of writing questions
OWASP top 10 vulnerability Guide (2021)
小程序中实现文章的关注功能
【FineBI】使用FineBI制作自定义地图过程
随机推荐
Threejs realizes rain, snow, overcast, sunny, flame
Use threejs to create geometry, dynamically add geometry, delete geometry, and add coordinate axes
Fuel consumption calculator
Technical tutorial: how to use easydss to push live streaming to qiniu cloud?
laravel8 导出Excle文件
A應用喚醒B應該快速方法
[finebi] the process of making custom maps using finebi
Machine learning decision tree
CTF stegano practice stegano 9
[phantom engine UE] realize the animation production of mapping tripod deployment
Online text line fixed length fill tool
行为感知系统
Use threejs to create geometry and add materials, lights, shadows, animations, and axes
[understand series after reading] 6000 words teach you to realize interface automation from 0 to 1
阿里云ECS使用cloudfs4oss挂载OSS
Learning MVVM notes (1)
Differences among 10 addressing modes
mysql的七种join连接查询
Threejs factory model 3DMAX model obj+mtl format, source file download
Threejs loads the city obj model, loads the character gltf model, and tweetjs realizes the movement of characters according to the planned route