反思:使用指针前先分配内存。
#include<bits/stdc++.h>
using namespace std;
typedef struct node
{int data;struct node* left;struct node* right;
}*T;
queue<int>q1;
queue<int>q2;
queue<int>q3;
T result;
void built1(T &t,int x)
{if(t==NULL){t=new node;t->data=x;t->left=NULL;t->right=NULL;}else {if(t->data<=x)built1(t->right,x);elsebuilt1(t->left,x);}
}
void built2(T &t,int x)
{if(t==NULL){t=new node;t->data=x;t->left=NULL;t->right=NULL;}else {if(t->data<=x)built2(t->left,x);elsebuilt2(t->right,x);}
}
bool judge(T t)
{if(t){int x=q3.front();q3.pop();if(x!=t->data)return 0;if(!judge(t->left))return 0;if(!judge(t->right))return 0;}return 1;
}
bool work()
{T t1=NULL,t2=NULL;while(!q1.empty()){int x=q1.front();q1.pop();built1(t1,x);}if(judge(t1)){result=t1;return 1;}q3=q2;while(!q2.empty()){int x=q2.front();q2.pop();built2(t2,x);}if(judge(t2)){result=t2;return 1;}return 0;
}
int cot=0;
void out(T t)
{if(t!=NULL){ out(t->left);out(t->right);if(!cot)cout<<t->data;elsecout<<' '<<t->data; cot++;}
}
int main()
{int n;cin>>n;for(int i=0;i<n;i++){int x;cin>>x;q1.push(x);}q3=q2=q1;result=new node;if(work()){cout<<"YES"<<endl;out(result);}else cout<<"NO"<<endl;
}