当前位置:网站首页>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 !
边栏推荐
- 智能终端设备加密防护的意义和措施
- How to estimate the number of threads
- C # create database connection object SQLite database
- MES, APS and ERP are essential to realize fine production
- Database addition, deletion, modification and query
- Word delete the contents in brackets
- When the Jericho development board is powered on, you can open the NRF app with your mobile phone [article]
- 洛谷P4127 [AHOI2009]同类分布 题解
- [cf gym101196-i] waif until dark network maximum flow
- [非线性控制理论]9_非线性控制理论串讲
猜你喜欢
How to delete all the words before or after a symbol in word
The way to learn go (I) the basic introduction of go to the first HelloWorld
octomap averageNodeColor函数说明
[MySQL learning notes 30] lock (non tutorial)
If Jerry's Bluetooth device wants to send data to the mobile phone, the mobile phone needs to open the notify channel first [article]
[1. Delphi foundation] 1 Introduction to Delphi Programming
智能终端设备加密防护的意义和措施
Ble of Jerry [chapter]
Opencv learning notes 8 -- answer sheet recognition
[非线性控制理论]9_非线性控制理论串讲
随机推荐
Position() function in XPath uses
Typescript indexable type
成为优秀的TS体操高手 之 TS 类型体操前置知识储备
TS 类型体操 之 extends,Equal,Alike 使用场景和实现对比
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]
Key value judgment in the cycle of TS type gymnastics, as keyword use
实现精细化生产, MES、APS、ERP必不可少
Mise en œuvre du langage leecode - C - 15. Somme des trois chiffres - - - - - idées à améliorer
qt颜色与字符串、uint相互转换
杰理之BLE【篇】
杰理之BLE【篇】
Openjudge noi 2.1 1749: Digital Square
C intercept string
MEX有关的学习
杰理之BLE【篇】
Memory error during variable parameter overload
学go之路(一)go的基本介绍到第一个helloworld
How Navicat imports MySQL scripts
杰理之普通透传测试---做数传搭配 APP 通信【篇】