当前位置:网站首页>剑指 Offer 07. 重建二叉树
剑指 Offer 07. 重建二叉树
2022-06-25 15:32:00 【anieoo】
原题链接:剑指 Offer 07. 重建二叉树
soluition:
105. 从前序与中序遍历序列构造二叉树_anieoo的博客-CSDN博客
class Solution {
public:
unordered_map<int,int> pos;
TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {
for(int i = 0;i < inorder.size();i++)
pos[inorder[i]] = i; //记录每个节点在中序数组中的位置
return Build(preorder, 0, preorder.size() - 1, inorder, 0, inorder.size() - 1);
}
TreeNode *Build(vector<int> &pre, int p_l, int p_r, vector<int> &ino, int in_l, int in_r) {
if(p_l > p_r) return nullptr;
int k = pos[pre[p_l]] - in_l;
TreeNode *root = new TreeNode(pre[p_l]);
root->left = Build(pre, p_l + 1, p_l + k, ino, in_l, in_l + k - 1);
root->right = Build(pre, p_l + k + 1, p_r, ino, in_l + k + 1, in_r);
return root;
}
};
边栏推荐
- CV pre training model set
- A deformation problem of Hanoi Tower
- Bessie's weight problem [01 backpack]
- About%*s and%* s
- Paddlepaddle paper reproduction course biggan learning experience
- Detailed description of crontab command format and summary of common writing methods
- 股票开户用什么app最安全?知道的给说一下吧
- Basic syntax and common commands of R language
- The difference between sizeof and strlen
- Stderr and stdout related standard outputs and other C system APIs
猜你喜欢

Several common optimization methods

Advertising effect cluster analysis (kmeans)
![[paper notes] street view change detection with deconvolutional networks](/img/2d/777fd0d85ff4d349516b95923410fd.jpg)
[paper notes] street view change detection with deconvolutional networks

(2) Relational database

Fishing detection software

semget No space left on device

GDB debugging

Simulating Sir disease transmission model with netlogo

System Verilog - thread

Data feature analysis skills - correlation test
随机推荐
[paper notes] poly yolo: higher speed, more precise detection and instance segmentation for yolov3
[C language] implementation of magic square array (the most complete)
Stderr and stdout related standard outputs and other C system APIs
One code per day - day one
Day01: learning notes
Afterword of Parl intensive learning 7-day punch in camp
Learning notes on February 8, 2022 (C language)
Common dynamic memory errors
剑指 Offer 03. 数组中重复的数字
Statistical analysis - data level description of descriptive statistics
Pytorch distributed test pit summary
Summary of regularization methods
QT animation loading and closing window
The last glory of the late Ming Dynasty - the battle of Korea
通过客户经理的开户链接开股票账户安全吗?
Bessie's weight problem [01 backpack]
Joseph Ring - formula method (recursive formula)
Install Kali extension 1: (kali resolution problem)
Completabilefuture of asynchronous tools for concurrent programming
Record the time to read the file (the system cannot find the specified path)