本文共 562 字,大约阅读时间需要 1 分钟。
4个数组保存一棵树
2个保存左右儿子
1个保存值
1个保存指针
#include<iostream>#include<cstdio>#include<cstring>using namespace std;int a[100010][4];void DFS(int x){ if(x==0)return; cout<<x<<' '; if(a[x][2])DFS(a[x][2]); if(a[x][3])DFS(a[x][3]); return;}int main(){ ios::sync_with_stdio(false); int i,n,len=0,tem; memset(a,0,sizeof(a)); for(cin>>n,i=1; i<=n; i++)cin>>tem,a[tem][0]=i; for(i=1; i<=n; i++) { for(tem=len;tem&&a[a[tem][1]][0]>a[i][0];tem--); if(tem)a[a[tem][1]][3]=i; if(tem<len)a[i][2]=a[tem+1][1]; a[len=++tem][1]=i; } DFS(a[1][1]); return 0;}
转载地址:http://wrze.baihongyu.com/