当前位置:网站首页>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 !
边栏推荐
- CF1036C Classy Numbers 题解
- 成为优秀的TS体操高手 之 TS 类型体操前置知识储备
- Le chemin du navigateur Edge obtient
- Excel的相关操作
- Compliance and efficiency, accelerate the digital transformation of pharmaceutical enterprises, and create a new document resource center for pharmaceutical enterprises
- 烧录场景下的源代码防泄密方案分享
- Ble of Jerry [chapter]
- After the hot update of uniapp, "mismatched versions may cause application exceptions" causes and Solutions
- datax自检报错 /datax/plugin/reader/._drdsreader/plugin.json]不存在
- 杰理之BLE【篇】
猜你喜欢
继电反馈PID控制器参数自整定
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
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
Ble of Jerry [chapter]
Google可能在春节后回归中国市场。
Generator Foundation
学go之路(一)go的基本介绍到第一个helloworld
Ble of Jerry [chapter]
[online problem processing] how to kill the corresponding process when the MySQL table deadlock is caused by the code
成为优秀的TS体操高手 之 TS 类型体操前置知识储备
随机推荐
[非线性控制理论]9_非线性控制理论串讲
TypeScript 接口属性
OpenJudge NOI 2.1 1661:Bomb Game
Google may return to the Chinese market after the Spring Festival.
Simulation of Michelson interferometer based on MATLAB
The difference between TS Gymnastics (cross operation) and interface inheritance
GET/POST/PUT/PATCH/DELETE含义
Luogu p1836 number page solution
上线APS系统,破除物料采购计划与生产实际脱钩的难题
软件测试界的三无简历,企业拿什么来招聘你,石沉大海的简历
[1. Delphi foundation] 1 Introduction to Delphi Programming
Typescript function definition
Typescript interface and the use of generics
js对象获取属性的方法(.和[]方式)
Launch APS system to break the problem of decoupling material procurement plan from production practice
Summary of Digital IC design written examination questions (I)
Select all the lines with a symbol in word and change them to titles
Le chemin du navigateur Edge obtient
Description of octomap averagenodecolor function
Mise en œuvre du langage leecode - C - 15. Somme des trois chiffres - - - - - idées à améliorer