当前位置:网站首页>Special topic of binary tree -- acwing 18 Rebuild the binary tree (construct the binary tree by traversing the front and middle order)
Special topic of binary tree -- acwing 18 Rebuild the binary tree (construct the binary tree by traversing the front and middle order)
2022-07-02 10:58:00 【Morgannr】

The idea and code are similar to the previous question AcWing 1497. Tree traversal , I won't go into too much detail here .
Code :
class Solution {
public:
#define map unordered_map
map<int, int> d;
vector<int> pre, in;
TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {
pre = preorder, in = inorder;
int n = inorder.size();
for (int i = 0; i < n; ++i)
{
d[in[i]] = i;
}
return dfs(0, n - 1, 0, n - 1);
}
TreeNode* dfs(int pl, int pr, int il, int ir)
{
if (pl > pr) return nullptr;
auto root = new TreeNode(pre[pl]);
int k = d[root->val];
TreeNode* le, * ri;
le = dfs(pl + 1, k - il + pl, il, k - 1);
ri = dfs(k - il + pl + 1, pr, k + 1, ir);
root->left = le, root->right = ri;
return root;
}
};
边栏推荐
- JSP webshell免杀——webshell免杀
- 华为快应用中如何实现同时传递事件对象和自定义参数
- Introduction to MySQL 8 DBA foundation tutorial
- 对话吴纲:我为什么笃信“大国品牌”的崛起?
- 1287_ Implementation analysis of prvtaskistasksuspended() interface in FreeRTOS
- PCL之K-d树与八叉树
- 二叉树专题--P1030 [NOIP2001 普及组] 求先序排列
- Shutter - canvas custom graph
- Thanos Receiver
- shell编程01_Shell基础
猜你喜欢

Hdu1236 ranking (structure Sorting)

The nanny level tutorial of flutter environment configuration makes the doctor green to the end

Read H264 parameters from mediarecord recording

Nodejs+express+mysql simple blog building

1287_FreeRTOS中prvTaskIsTaskSuspended()接口实现分析
![[visual studio] visual studio 2019 community version cmake development environment installation (download | install relevant components | create compilation execution project | error handling)](/img/9f/4265f1e3927fcf66602f0fc9e7a9d9.jpg)
[visual studio] visual studio 2019 community version cmake development environment installation (download | install relevant components | create compilation execution project | error handling)

12.进程同步与信号量

华为游戏初始化init失败,返回错误码907135000

JSP webshell免杀——webshell免杀

MYSQL环境配置
随机推荐
洛谷 P1892 [BOI2003]团伙(并查集变种 反集)
【AppLinking实战案例】通过AppLinking分享应用内图片
拆解美图SaaS:开着飞机换引擎
PCL 点云转深度图像
使用sqlcipher打开加密的sqlite方法
MySQL keyword
PCL extracts a subset from a point cloud
1287_ Implementation analysis of prvtaskistasksuspended() interface in FreeRTOS
点云投影图片
Introduction to MySQL 8 DBA foundation tutorial
二叉树专题--AcWing 19. 二叉树的下一个节点(找树中节点的后继)
Dialogue Wu Gang: why do I believe in the rise of "big country brands"?
P1055 [NOIP2008 普及组] ISBN 号码
《实习报告》Skywalking分布式链路追踪?
华为AppLinking中统一链接的创建和使用
SPSS做Shapiro-Wilk正态分析
LabVIEW为什么浮点数会丢失精度
最详细MySql安装教程
13.信号量临界区保护
MySQL数据库远程访问权限设置