当前位置:网站首页>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;
}
}
边栏推荐
- [phantom engine UE] realize the animation production of mapping tripod deployment
- PR video clip (project packaging)
- 快手、抖音、视频号交战内容付费
- [phantom engine UE] only six steps are needed to realize the deployment of ue5 pixel stream and avoid detours! (the principles of 4.26 and 4.27 are similar)
- Serpentine matrix
- Threejs realizes rain, snow, overcast, sunny, flame
- A应用唤醒B应该快速方法
- 美国5G Open RAN再遭重大挫败,抗衡中国5G技术的图谋已告失败
- 【虚幻引擎UE】实现UE5像素流部署仅需六步操作少走弯路!(4.26和4.27原理类似)
- 北京程序员的真实一天!!!!!
猜你喜欢
Use of vscode software
电源管理总线 (PMBus)
Alibaba cloud ECS uses cloudfs4oss to mount OSS
基于TCP的移动端IM即时通讯开发仍然需要心跳保活
Threejs Internet of things, 3D visualization of farms (I)
25K 入职腾讯的那天,我特么哭了
防护电路中的元器件
[phantom engine UE] realize the animation production of mapping tripod deployment
【虚幻引擎UE】实现测绘三脚架展开动画制作
Seven join join queries of MySQL
随机推荐
C language course setting: cinema ticket selling management system
MySQL: view with subquery in the from clause limit
【虚幻引擎UE】实现背景模糊下近景旋转操作物体的方法及踩坑记录
How to solve the problem that easycvr changes the recording storage path and does not generate recording files?
Online text line fixed length fill tool
Learning notes 8
Decimal to hexadecimal
Threejs Internet of things, 3D visualization of farms (I)
OWASP top 10 vulnerability Guide (2021)
Number of possible stack order types of stack order with length n
[untitled]
10种寻址方式之间的区别
Rome chain analysis
mxnet导入报各种libcudart*.so、 libcuda*.so找不到
[understand series after reading] 6000 words teach you to realize interface automation from 0 to 1
基于TCP的移动端IM即时通讯开发仍然需要心跳保活
Technical tutorial: how to use easydss to push live streaming to qiniu cloud?
Threejs Internet of things, 3D visualization of farms (II)
Interview related high-frequency algorithm test site 3
provide/inject