#include<iostream>
#include<cstring>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
const int maxn = 510;
int w[maxn];
int l[maxn], r[maxn], v[maxn], idx;
int tr[maxn];
int n;
bool flag = true;
bool is_leaf;
void insert(int u, int x)
{
if(!v[u]) v[u] = x;
else
{
if(x > v[u]) {
if(!l[u]) l[u] = ++ idx;
insert(l[u], x);
}else{
if(!r[u]) r[u] = ++ idx;
insert(r[u], x);
}
}
}
void bfs(int u)
{
queue<int> q;
q.push(u);
vector<int> res;
while(q.size())
{
auto t = q.front();
q.pop();
res.push_back(t);
if(l[t]) q.push(l[t]);
if(r[t]) q.push(r[t]);
if(is_leaf && (l[t] || r[t])) flag = false;
if(!l[t] && r[t]) flag = false;
if(!r[t]) is_leaf = true;
}
for(int i = 0; i < res.size(); i ++){
if(i != 0) cout << " ";
cout << v[res[i]];
}
cout << endl;
}
int main()
{
cin >> n;
idx = 1;
for(int i = 1; i <= n; i ++){
int x; cin >> x;
w[i] = x;
insert(1, x);
}
bfs(1);
if(flag) cout << "YES" << endl;
else cout << "NO" << endl;
return 0;
}
|