题目描述
知识点: 二叉搜索树
思路: 一道建立二叉搜索树的模板题,然后在建树的过程中记录各层的节点数即可,然后找到最大层以及上一层输出数量就可以了。
#include<iostream>
using namespace std;
const int N = 2100;
struct Node{
int x;
Node *left,*right;
};
int n;
int h[N];
Node *insert(Node *u,int x,int l){
if(u == NULL) {
u = new Node({x,NULL,NULL});
h[l]++;
return u;
}
else{
if(u->x>=x) u->left = insert(u->left,x,l + 1);
else u->right = insert(u->right,x,l + 1);
}
return u;
}
int main(){
cin>>n;
Node *root = NULL;
for(int i = 0;i < n;i++){
int c;
cin>>c;
root = insert(root,c,1);
}
int res = 0;
for(int i = n;i >= 0;i--){
if(h[i] != 0){
res = i;
break;
}
}
cout<<h[res]<<" + "<<h[res-1]<<" = "<<h[res] + h[res-1];
return 0;
}