资深光能
#include<iostream>
using namespace std;
struct node{
int v;
int left,right;
}tree[300010];
int size=1000,n,a[300010];
int newnode(int v){
size++;
tree[size].v=v;
tree[size].left=tree[size].right=-1;
return size;
}
void insert(int& idx,int x){
if(idx==-1||idx==0){
idx=newnode(x);
return ;
}
cout<<x<<' ';
if(x<tree[idx].v)insert(tree[idx].left,x);
else insert(tree[idx].right,x);
}
int find(int idx,int x){
if(tree[idx].v==x)return idx;
if(idx=-1)return -1;
if(x<tree[idx].v)return find(tree[idx].left,x);
else return find(tree[idx].right,x);
}
int maxfind(int idx){
if(tree[idx].right==-1)return idx;
return maxfind(tree[idx].right);
}
void del(int x,int& idx)
{
if(idx==-1)return ;
if(tree[idx].v==x){
if(tree[idx].left==-1&&tree[idx].right==-1)idx=-1;
else if(tree[idx].left==-1)
idx=tree[idx].right;
else if(tree[idx].right==-1)
idx=tree[idx].left;
else{
int maxidx=maxfind(tree[idx].left);
tree[idx].v=tree[maxidx].v;
del(tree[maxidx].v,tree[idx].left);
}
}
else{
if(x<tree[idx].v)del(x,tree[idx].left);
else del(x,tree[idx].right);
}
}
int s=-1;
void A(int x,int idx){
if(idx==-1)return ;
s=max(s,x);
A(x+1,tree[idx].left);
A(x+1,tree[idx].right);
// cout<<tree[idx].v;
}
void B(int idx){
if(idx==-1)return ;
B(tree[idx].left);
B(tree[idx].right);
cout<<tree[idx].v<<endl;
}
int main()
{
cin>>n;
for(int i=1;i<=n;i++){
cin>>a[i];
int x=1;
insert(x,a[i]);
}
A(1,1);
cout<<"deep="<<s;
B(1);
}
爆0