当前位置:网站首页>剑指 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;
}
};
边栏推荐
- QT animation loading and closing window
- Distributed transaction solution
- Is it safe to open a stock account online?
- 程序员 VS 黑客的思维 | 每日趣闻
- (translation) json-rpc 2.0 specification (Chinese version)
- Is Guoxin golden sun reliable? Is it legal? Is it safe to open a stock account?
- Internal class learning notes
- MySQL performance optimization - index optimization
- Summary of four parameter adjustment methods for machine learning
- Is it safe to open an account for new bonds? What preparations are needed
猜你喜欢

QT pattern prompt box implementation

(2) Relational database

Design and implementation of thread pool

剑指 Offer 06. 从尾到头打印链表

Arthas, a sharp tool for online diagnosis - several important commands

程序员 VS 黑客的思维 | 每日趣闻

MySQL performance optimization - index optimization

Paddlepaddle paper reproduction course biggan learning experience

5 connection modes of QT signal slot

User defined data type - structure
随机推荐
Basic syntax and common commands of R language
MySQL performance optimization - index optimization
Go language modifies / removes multiple line breaks in strings
Stderr and stdout related standard outputs and other C system APIs
0706-- replace fields in the use case, such as mobile phone number or ID
Kali SSH Remote Login
JS select all exercise
Summary of regularization methods
Websocket (WS) cluster solution
Could not connect to redis at 127.0.0.1:6379 in Windows
Solution of push code failure in idea
Why do I need message idempotence?
AB string interchange
QT set process startup and self startup
System Verilog - function and task
semget No space left on device
Ubuntu 20.04 installing mysql8.0 and modifying the MySQL password
Advertising effect cluster analysis (kmeans)
Go language template text/template error unexpected EOF
MySQL field truncation principle and source code analysis