当前位置:网站首页>剑指 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;
}
};
边栏推荐
- The robot is playing an old DOS based game
- System Verilog - function and task
- Simulating Sir disease transmission model with netlogo
- semget No space left on device
- Shared memory synchronous encapsulation
- Is it safe to open a stock account through the account opening link given by the account manager? I want to open an account
- The situation and suggestions of a software engineering associate graduate who failed in the postgraduate entrance examination
- Is it safe to open a stock account in Guoxin golden sun?
- MySQL performance optimization - index optimization
- 到底要不要去外包公司?这篇带你全面了解外包那些坑!
猜你喜欢

Common dynamic memory errors

Source code analysis of synergetics and ntyco

Graphic control and layout basis of R visualization

Using R language in jupyter notebook

Learning notes on February 5, 2022 (C language)

Mining procedure processing

‘make_ unique’ is not a member of ‘std’

Source code analysis of zeromq lockless queue

JS select all exercise

Single user mode
随机推荐
Learning to Measure Changes: Fully Convolutional Siamese Metric Networks for Scene Change Detection
[paper notes] poly yolo: higher speed, more precise detection and instance segmentation for yolov3
Several common optimization methods
Go language template text/template error unexpected EOF
Boost listening port server
Principle and implementation of MySQL master-slave replication (docker Implementation)
Detailed summary of reasons why alertmanager fails to send alarm messages at specified intervals / irregularly
Advertising effect cluster analysis (kmeans)
Source code analysis of zeromq lockless queue
国信金太阳靠谱吗?是否合法?开股票账户安全吗?
55 specific ways to improve program design (1)
Install Kali extension 1: (kali resolution problem)
CV pre training model set
Using R language in jupyter notebook
Joseph Ring - formula method (recursive formula)
QT animation loading and closing window
System Verilog - thread
5 connection modes of QT signal slot
Mining procedure processing
Dynamic memory allocation