当前位置:网站首页>Nc204382 medium sequence
Nc204382 medium sequence
2022-07-06 07:39:00 【cq. tiancx】
subject : NC204382 Middle order sequence , ha-ha , Let's look at a classic binary tree problem today , This is a question from Niuke , Okay , Let's have a look at the meaning of the title :
The title description is copied , There may be some display errors , I'll put the title link below !
Topic link : NC204382 Middle order sequence
Be careful : This problem is the core code pattern , It's the submission mode of helikou !
Title Description
Given a tree with n The sequence of preorder traversal and postorder traversal of a binary tree with nodes , Find the middle order traversal sequence . If a node has only one child node , Then it is regarded as the left son nodeExample 1
Input
5,[3,2,1,4,5],[1,5,4,2,3]
Output
[1,2,5,4,3]
Let's look at success AC The code of :
vector<int> v;
void dfs(int l1,int r1,int l2,int r2,vector<int>& pre, vector<int>& suf){
// if(l1>r1) return ;
if(l1==r1) {
v.push_back(pre[l1]); return ;}
int pos=-1;
int x=pre[l1+1];
for(int i=l2;i<=r2;i++){
if(suf[i]==x) pos=i;
}
dfs(l1+1,l1+1+pos-l2,l2,pos,pre,suf);
v.push_back(pre[l1]);
if(pos+1<=r2-1) dfs(l1+1+pos-l2+1,r1,pos+1,r2-1,pre,suf);
}
class Solution {
public:
/** * The class name in the code 、 Method name 、 The parameter name has been specified , Do not modify , Return the value specified by the method directly * * * @param n int Number of binary tree nodes * @param pre intvector Preorder sequence * @param suf intvector Subsequent sequence * @return intvector */
vector<int> solve(int n, vector<int>& pre, vector<int>& suf) {
// write code here
dfs(0,n-1,0,n-1,pre,suf);
return v;
}
};
Thanks for reading
, Because the author level is limited , There are inevitably shortcomings , If the reader finds a problem , Please criticize , Leave a message in the message area or send a private message to inform , I will revise it as soon as possible . If you guys have any good solutions , Or meaningful solutions can be displayed in the comment area , Thank you very much .
Writing is not easy to , I hope all bosses will praise me , Add a focus on !
边栏推荐
- If Jerry needs to send a large package, he needs to modify the MTU on the mobile terminal [article]
- GET/POST/PUT/PATCH/DELETE含义
- Word delete the contents in brackets
- 2022年Instagram运营小技巧简单讲解
- Le chemin du navigateur Edge obtient
- TypeScript void 基础类型
- Oracle column to row -- a field is converted to multiple rows according to the specified separator
- 软件开发的一点随记
- Launch APS system to break the problem of decoupling material procurement plan from production practice
- Scala language learning-08-abstract classes
猜你喜欢
Bugku CTF daily question: do you want seeds? Blackmailed
数字经济时代,如何保障安全?
杰理之开发板上电开机,就可以手机打开 NRF 的 APP【篇】
Leecode-c language implementation -15 Sum of three ----- ideas to be improved
QT color is converted to string and uint
In the era of digital economy, how to ensure security?
Basics of reptile - Scratch reptile
烧录场景下的源代码防泄密方案分享
If Jerry needs to send a large package, he needs to modify the MTU on the mobile terminal [article]
Ali's redis interview question is too difficult, isn't it? I was pressed on the ground and rubbed
随机推荐
Google may return to the Chinese market after the Spring Festival.
Apache middleware vulnerability recurrence
Cf1036c class numbers solution
Word delete the contents in brackets
Bugku CTF daily question: do you want seeds? Blackmailed
C # connect to SQLite database to read content
Typescript interface properties
位运算异或
word中把带有某个符号的行全部选中,更改为标题
实现精细化生产, MES、APS、ERP必不可少
The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
[cf gym101196-i] waif until dark network maximum flow
成为优秀的TS体操高手 之 TS 类型体操前置知识储备
Ble of Jerry [chapter]
word删除括号里内容
P3047 [USACO12FEB]Nearby Cows G(树形dp)
Scala language learning-08-abstract classes
[window] when the Microsoft Store is deleted locally, how to reinstall it in three steps
Jerry's ad series MIDI function description [chapter]
TypeScript 接口属性