做题历程:
拿到手就开始建树,结点太多了所以不能数组建树,就结构体建树,建完发现还得建镜像,就懒得干了,去找大佬们的题解,发现大家都是直接通过二叉搜索树的性质来递归左右子树,一气呵成......还是做题做少了,照葫芦画瓢才过的。。。
AcCode:
#include<iostream>
#include<vector>
using namespace std;
int font[1010];
vector<int> ans;
vector<int> ans2;
void check_1(int l, int r){if(l > r) return ;int rt = font[l];int be = l + 1;int end = r;while(be <= end && rt > font[be]) be++;while(be <= end && rt <= font[end]) end--;if(be - end != 1) return ;check_1(l + 1, end);check_1(be, r);ans.push_back(rt);
}void check_2(int l, int r){if(l > r) return ;int rt = font[l];int be = l + 1;int end = r;while(be <= end && rt <= font[be]) be++;while(be <= end && rt > font[end]) end--;if(be - end != 1) return ;check_2(l + 1, end);check_2(be, r);ans2.push_back(rt);
}int main(){int N;cin >> N;for(int i = 0; i < N; i++) cin >> font[i];check_1(0, N - 1);check_2(0, N - 1);if(ans.size() != N && ans2.size() != N){cout << "NO" << endl;}else{cout << "YES" << endl;if(ans.size() == N){for(int i = 0; i < ans.size(); i++){cout << ans[i];if(i != ans.size() - 1) cout << " ";}}else{for(int i = 0; i < ans2.size(); i++){cout << ans2[i];if(i != ans2.size() - 1) cout << " ";}}}return 0;
}