
java
import java.util.Scanner;
public class Main {
static class Node {
int val;
Node left, right;
Node(int x) { val = x; }
}
// 构建BST或其镜像,isMirror=true表示镜像
private static Node buildBST(int[] pre, int start, int end, boolean isMirror) {
if (start > end) return null;
Node root = new Node(pre[start]);
if (start == end) return root;
int split = start + 1;
if (!isMirror) {
// 普通BST规则:左子树 < 根,右子树 >= 根
while (split <= end && pre[split] < root.val) {
split++;
}
// 验证右子树所有节点都 >= 根
for (int i = split; i <= end; i++) {
if (pre[i] < root.val) return null;
}
} else {
// 镜像BST规则:左子树 >= 根,右子树 < 根
while (split <= end && pre[split] >= root.val) {
split++;
}
// 验证右子树所有节点都 < 根
for (int i = split; i <= end; i++) {
if (pre[i] >= root.val) return null;
}
}
// 递归构建左右子树
root.left = buildBST(pre, start + 1, split - 1, isMirror);
if (root.left == null && (start + 1 <= split - 1)) return null;
root.right = buildBST(pre, split, end, isMirror);
if (root.right == null && (split <= end)) return null;
return root;
}
// 后序遍历
private static void postOrder(Node root, StringBuilder sb) {
if (root == null) return;
postOrder(root.left, sb);
postOrder(root.right, sb);
if (sb.length() > 0) sb.append(" ");
sb.append(root.val);
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
int[] pre = new int[N];
for (int i = 0; i < N; i++) {
pre[i] = sc.nextInt();
}
sc.close();
Node root = buildBST(pre, 0, N - 1, false);
boolean isNormalBST = root != null;
if (!isNormalBST) {
root = buildBST(pre, 0, N - 1, true);
}
if (root != null) {
System.out.println("YES");
StringBuilder sb = new StringBuilder();
postOrder(root, sb);
System.out.println(sb);
} else {
System.out.println("NO");
}
}
}