经典问题之判断是否是二叉搜索树:二叉搜索树中序遍历有序or....
这道题需要重新看
这里判断:(参考柳婼)
//柳婼
#include<bits/stdc++.h>
using namespace std;
int n;
int pre[1010];
vector<int> post;
bool isMirror=false;
void dfs(int l,int r){
if(l>r) return;
int root=pre[l];
int i=l+1,j=r;
if(isMirror==false){
while(pre[i]<root&&i<=r) i++;
while(pre[j]>=root&&j>l) j--;
}else{
while(pre[i]>=root&&i<=r) i++;
while(pre[j]<root&&j>l) j--;
}
if(i-j!=1) return;
//左子树
dfs(l+1,j);
dfs(i,r);
post.push_back(root);
}
int main(){
cin>>n;
for(int i=0;i<n;i++) cin>>pre[i];
dfs(0, n - 1);
if(post.size() != n) {
isMirror = true;
post.clear();
dfs(0, n - 1);
}
if(post.size() &