当前位置:网站首页>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 !
边栏推荐
- Excel的相关操作
- Typescript void base type
- 实现精细化生产, MES、APS、ERP必不可少
- Set picture annotation in markdown
- 学go之路(二)基本类型及变量、常量
- Scala language learning-08-abstract classes
- Brief explanation of instagram operation tips in 2022
- C # display the list control, select the file to obtain the file path and filter the file extension, and RichTextBox displays the data
- How to delete all the words before or after a symbol in word
- Opencv learning notes 9 -- background modeling + optical flow estimation
猜你喜欢
Compliance and efficiency, accelerate the digital transformation of pharmaceutical enterprises, and create a new document resource center for pharmaceutical enterprises
剪映的相关介绍
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
[非线性控制理论]9_非线性控制理论串讲
合规、高效,加快药企数字化转型,全新打造药企文档资源中心
qt颜色与字符串、uint相互转换
Oracle column to row -- a field is converted to multiple rows according to the specified separator
解决方案:智慧工地智能巡檢方案視頻監控系統
【mysql学习笔记30】锁(非教程)
Ble of Jerry [chapter]
随机推荐
Luogu p4127 [ahoi2009] similar distribution problem solution
Select all the lines with a symbol in word and change them to titles
Typescript void base type
Jerry's ad series MIDI function description [chapter]
Force buckle day31
杰理之开发板上电开机,就可以手机打开 NRF 的 APP【篇】
opencv学习笔记八--答题卡识别
Brief explanation of instagram operation tips in 2022
Ble of Jerry [chapter]
杰理之BLE【篇】
Key value judgment in the cycle of TS type gymnastics, as keyword use
1015 reversible primes (20 points) prime d-ary
软件测试界的三无简历,企业拿什么来招聘你,石沉大海的简历
When the Jericho development board is powered on, you can open the NRF app with your mobile phone [article]
C # display the list control, select the file to obtain the file path and filter the file extension, and RichTextBox displays the data
TypeScript 可索引类型
Jerry needs to modify the profile definition of GATT [chapter]
NiO programming introduction
QT color is converted to string and uint
Solution: intelligent site intelligent inspection scheme video monitoring system