题目链接:点击打开链接
思路:题目不难,就是麻烦一些,创建完二叉树后,读取陈述句判断即可。
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <map>
using namespace std;
int data[105],le[105],rig[105];
int fa[105],level[105];
int root;
map<int,int> m;
void add(int &r,int father,int depth,int loc){
if(r == -1){
r = loc;
fa[loc] = father;
level[loc] = depth;
return;
}
if(data[loc] < data[r]){
add(le[r],r,depth + 1,loc);
}
else{
add(rig[r],r,depth + 1,loc);
}
}
int main(){
int n;
scanf("%d",&n);
root = -1;
memset(le,-1,sizeof(le));
memset(rig,-1,sizeof(rig));
for(int i = 1;i <= n;i++){
scanf("%d",&data[i]);
add(root,-1,1,i);
m[data[i]] = i;
}
int q,a,b;
string aa;
bool flag;
scanf("%d"