//柳姐解法:dfs,关于最大层数明显易多了
#include <iostream>
#include <vector>
#include <math.h>
using namespace std;
struct node
{
int data;
int height;
node* lchild,* rchild;
};
node* InsertT(node* root,int x){
if(!root){
root=new node;
root->data=x;
root->lchild=root->rchild=NULL;
return root;
}
if(root->data>=x)
root->lchild=InsertT(root->lchild,x);
else
root->rchild=InsertT(root->rchild,x);
return root;
}
vector<int> num;
int max_depth=0;
void dfs(node* root,int depth){
if(!root){
max_depth=max(depth,max_depth);//退出时max_depth要多1
return;
}
num[depth]++;
dfs(root->lchild,depth+1);
dfs(root->rchild,depth+1);
}
int main(){
int n,t;
scanf("%d",&n);
node* root=NULL;
for(int i=0;i<n;i++){
scanf("%d",&t);
root=InsertT(root,t);
}
num.resize(n);
dfs(root,0);
printf("%d + %d = %d",num[max_depth-1],num[max_depth-2],num[max_depth-1]+num[max_depth-2]);
return 0;
}
//我的解法:bfs,利用同层的连续关系,求每层的元素个数
#include <iostream>
#include <queue>
using namespace std;
struct node
{
int data;
int height;
node* lchild,* rchild;
};
node* InsertT(node* root,int x){
if(!root){
root=new node;
root->data=x;
root->lchild=root->rchild=NULL;
return root;
}
if(root->data>=x)
root->lchild=InsertT(root->lchild,x);
else
root->rchild=InsertT(root->rchild,x);
return root;
}
int n1=0,n2=0;
int max_height=0;
void bfs(node* root){
if(!root) return;
queue<node*> q;
q.push(root);
root->height=0;
n1=0;
while(!q.empty()){
node* tmp=q.front();
if(tmp->height==max_height){
n2++;
}else{
n1=n2;
n2=1;
max_height=tmp->height;
}
q.pop();
if(tmp->lchild){
q.push(tmp->lchild);
tmp->lchild->height=tmp->height+1;
}
if(tmp->rchild){
q.push(tmp->rchild);
tmp->rchild->height=tmp->height+1;
}
}
}
int main(){
int n,t;
scanf("%d",&n);
node* root=NULL;
for(int i=0;i<n;i++){
scanf("%d",&t);
root=InsertT(root,t);
}
bfs(root);
printf("%d + %d = %d",n2,n1,n1+n2);
return 0;
}