当前位置:网站首页>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 !
边栏推荐
猜你喜欢

数字IC设计笔试题汇总(一)
![If Jerry needs to send a large package, he needs to modify the MTU on the mobile terminal [article]](/img/57/12a97ab3d2dabfaf06bbe1788450cf.png)
If Jerry needs to send a large package, he needs to modify the MTU on the mobile terminal [article]

Solution: intelligent site intelligent inspection scheme video monitoring system
![[online problem processing] how to kill the corresponding process when the MySQL table deadlock is caused by the code](/img/93/ec9de907cae4714038bb5f95aba52b.png)
[online problem processing] how to kill the corresponding process when the MySQL table deadlock is caused by the code

opencv学习笔记八--答题卡识别

Simulation of Teman green interferometer based on MATLAB
![If Jerry's Bluetooth device wants to send data to the mobile phone, the mobile phone needs to open the notify channel first [article]](/img/d6/92ad1c6f84415de6ab0dfd16cd6073.png)
If Jerry's Bluetooth device wants to send data to the mobile phone, the mobile phone needs to open the notify channel first [article]

继电反馈PID控制器参数自整定
![DataX self check error /datax/plugin/reader/_ drdsreader/plugin. Json] does not exist](/img/17/415e66d67afb055e94a966de25c2bc.png)
DataX self check error /datax/plugin/reader/_ drdsreader/plugin. Json] does not exist
![When the Jericho development board is powered on, you can open the NRF app with your mobile phone [article]](/img/3e/3d5bff87995b4a9fac093a6d9f9473.png)
When the Jericho development board is powered on, you can open the NRF app with your mobile phone [article]
随机推荐
Typescript void base type
Ali's redis interview question is too difficult, isn't it? I was pressed on the ground and rubbed
js對象獲取屬性的方法(.和[]方式)
Word setting directory
HTTP cache, forced cache, negotiated cache
Sharing of source code anti disclosure scheme under burning scenario
Description of octomap averagenodecolor function
1015 reversible primes (20 points) prime d-ary
Pre knowledge reserve of TS type gymnastics to become an excellent TS gymnastics master
Set picture annotation in markdown
Risk planning and identification of Oracle project management system
Méthode d'obtention des propriétés de l'objet JS (.Et [] méthodes)
杰理之普通透传测试---做数传搭配 APP 通信【篇】
octomap averageNodeColor函数说明
学go之路(二)基本类型及变量、常量
Wonderful use of TS type gymnastics string
【MySQL学习笔记32】mvcc
杰理之BLE【篇】
Google可能在春节后回归中国市场。
Do you really think binary search is easy