#include<iostream> #include<algorithm> using namespace std; struct TreeNode { int data; TreeNode *parent,*left,*right; }; TreeNode *pTree,*mpTree; int isok=1,isok2=1; int arr1[1010],arr2[1010],arr[1010],b1=0,b2=0; int pre_order(TreeNode *Tree) { if(Tree!=NULL) { arr1[b1++]=Tree->data; pre_order(Tree->left); pre_order(Tree->right); } } int pre_order2(TreeNode *Tree) { if(Tree!=NULL) { arr2[b2++]=Tree->data; pre_order2(Tree->left); pre_order2(Tree->right); } } int last_order(TreeNode *Tree) { if(Tree!=NULL) { last_order(Tree->left); last_order(Tree->right); arr1[b1++]=Tree->data; } } int main() { int n,p; cin>>n; for(int i=0; i<n; i++) { cin>>p; arr[i]=p; if(pTree == NULL) { pTree=new TreeNode; pTree->data=p; pTree->left=pTree->right=NULL; mpTree=new TreeNode; mpTree->data=p; mpTree->left=mpTree->right=NULL; } else { TreeNode *point = pTree; while(true) { if(point->data > p) { if(point->left==NULL) { point->left = new TreeNode; point->left->data=p; point->left->left=point->left->right=NULL; break; } else { point=point->left; continue; } } if(point->data <= p) { if(point->right==NULL) { point->right = new TreeNode; point->right->data=p; point->right->left=point->right->right=NULL; break; } else { point=point->right; continue; } } } TreeNode *pointm = mpTree; while(true) { if(pointm->data <= p) { if(pointm->left==NULL) { pointm->left = new TreeNode; pointm->left->data=p; pointm->left->left=pointm->left->right=NULL; break; } else { pointm=pointm->left; continue; } } if(pointm->data > p) { if(pointm->right==NULL) { pointm->right = new TreeNode; pointm->right->data=p; pointm->right->left=pointm->right->right=NULL; break; } else { pointm=pointm->right; continue; } } } } } pre_order(pTree); pre_order2(mpTree); for(int i=0;i<b1;i++){ if(arr1[i]!=arr[i]){ isok=0; break; } } for(int i=0;i<b1;i++){ if(arr2[i]!=arr[i]){ isok2=0; break; } } if(isok==1) { cout<<"YES"<<endl; b1=0; last_order(pTree); for(int i=0;i<b1-1;i++) cout<<arr1[i]<<" "; cout<<arr1[b1-1]<<endl; } else if(isok2==1){ cout<<"YES"<<endl; b1=0; last_order(mpTree); for(int i=0;i<b1-1;i++) cout<<arr1[i]<<" "; cout<<arr1[b1-1]<<endl; } else cout<< "NO" << endl; return 0; }
4 total views, 4 views today