//算法训练 绘制地图(先序 中序 后序遍历)
#include<bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=(a);i<=(b);(i++))
const int M=100000+10;
int x[M],z[M];
void tree(int l1,int r1,int l2,int r2){
if(l1>r1||l2>r2) return;
int root=x[l1];
int tem=l2;
while(z[tem]!=root) tem++;
int cnt=tem-l2;
tree(l1+1,l1+cnt,l2,tem-1);//递归求解左子树
tree(l1+cnt+1,r1,tem+1,r2);//递归求解右子树
cout<<root<<" ";//后序遍历,左,右,根输出
}
int main()
{
int n;
cin>>n;
rep(i,1,n) cin>>x[i];
rep(i,1,n) cin>>z[i];
tree(1,n,1,n);//先序左,先序右,中序左,中序右
return 0;
}
算法训练 绘制地图(先序 中序 后序遍历)
最新推荐文章于 2022-07-26 19:12:15 发布
4355

被折叠的 条评论
为什么被折叠?



