背景:
树和二叉树基本上都有先序、中序、后序、按层遍历等遍历顺序,给定中序和其它一种遍历的序列就可以确定一棵二叉树的结构。
经典例题:
https://www.acwing.com/problem/content/3601/
要求:前序+中序求后序
实现代码:
#include<bits/stdc++.h>
#define int long long
#define endl "\n"
#define fi first
#define se second
#define PII pair<int,int>
using namespace std;
const int N=1e6+5;
//int a[N];
void build(string s1,string s2){
if(s1.empty()) return ;//前序为空
char ch=s1[0];//每个子树中第一个数即是根节点(前序的特点)
int k=s2.find(ch);
build(s1.substr(1,k),s2.substr(0,k));//左子树
build(s1.substr(k+1),s2.substr(k+1));//右子树
cout << ch ;
}
void solve(){
string s1,s2;
while(cin >> s1 >> s2){
build(s1,s2);
cout << endl;
}
return ;
}
signed main(){
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
int t=1;
//cin >> t;
while(t--) solve();
return 0;
}
https://www.acwing.com/problem/content/5073/
要求:后序+中序求前序
实现代码:
#include<bits/stdc++.h>
#define int long long
#define endl "\n"
#define fi first
#define se second
#define PII pair<int,int>
using namespace std;
const int N=5e4+5;
map<int,int> a,b,p,ans;//数据超出1e9,要用map存储,用数组会段错误
int s=0;
void build(int la,int ra,int lb,int rb){
if(la>ra) return ;//树不成立直接return
int vis=a[ra];//后序的最后一个数是根节点(后序的特点)
int k=p[vis];
ans[++s]=vis;//前序遍历直接存储即可
build(la,la+k-1-lb,lb,k-1);//左子树
build(la+k-lb,ra-1,k+1,rb);//右子树
}
void solve(){
int n;
cin >> n;
for(int i=0;i<n;++i){//后序
int x;
cin >> x;
a[i]=x;//map映射
}
for(int i=0;i<n;++i){//中序
int x;
cin >> x;
b[i]=x;
}
for(int i=0;i<n;++i){
p[b[i]]=i;//标记下标
}
build(0,n-1,0,n-1);
//for(auto i : ans) cout << i << " ";
cout << ans[s] << endl;
return ;
}
signed main(){
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
int t=1;
//cin >> t;
while(t--) solve();
return 0;
}
https://www.acwing.com/problem/content/1499/
要求:后序+中序求层序
实现代码:
#include<bits/stdc++.h>
#define int long long
#define endl "\n"
#define fi first
#define se second
#define PII pair<int,int>
using namespace std;
const int N=5e4+5;
map<int,int> a,b,p;
vector<int> ans[N];
void build(int la,int ra,int lb,int rb,int d){
if(la>ra) return ;
int vis=a[ra];
int k=p[vis];
ans[d].push_back(vis);//层序遍历用二维数组来存储数据
build(la,la+k-1-lb,lb,k-1,d+1);
build(la+k-lb,ra-1,k+1,rb,d+1);
}
void solve(){
int n;
cin >> n;
for(int i=0;i<n;++i){
int x;
cin >> x;
a[i]=x;
}
for(int i=0;i<n;++i){
int x;
cin >> x;
b[i]=x;
}
for(int i=0;i<n;++i){
p[b[i]]=i;
}
build(0,n-1,0,n-1,1);
for(int i=1;i<=n;++i){//按层输出
for(auto x : ans[i]) cout << x << " ";
}
return ;
}
signed main(){
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
int t=1;
//cin >> t;
while(t--) solve();
return 0;
}
https://www.acwing.com/problem/content/1261/
要求:中序+层序求先序
实现代码:
#include<bits/stdc++.h>
#define int long long
#define endl "\n"
#define fi first
#define se second
#define PII pair<int,int>
using namespace std;
const int N=1e6+5;
string s1,s2;
void build(int l1,int r1,int l2,int r2){
int i,j;
for(i=l2;i<=r2;++i){
bool flag=false;
for(j=l1;j<=r1;++j){
if(s2[i]==s1[j]){//j的左边为左子树,右边为右子树(中序的特点)
cout << s2[i];//找到直接输出即可
flag=true;
break;
}
}
if(flag) break;//输出过直接break
}
if(j > l1) build(l1, j - 1, l2 + 1, r2);//左子树
if(j < r1) build(j + 1, r1, l2 + 1, r2);//右子树
}
void solve(){
cin >> s1 >> s2;
int n=s1.size();
build(0,n-1,0,n-1);
return ;
}
signed main(){
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
int t=1;
//cin >> t;
while(t--) solve();
return 0;
}