刚刚学会手搓链表,就想着做个死
#include<bits/stdc++.h>
using namespace std;
int read(){
int s=0,w=1;
char ch=getchar();
while(ch<'0'||ch>'9'){
if(ch=='-'){
w=(~w)+1;
}
ch=getchar();
}
while(ch>='0'&&ch<='9'){
s=(s<<3)+(s<<1)+(ch&15);
ch=getchar();
}
return s*w;
}
void write(int x){
if(x<0){
putchar('-');
x=(~x)+1;
}
if(x>9){
write(x/10);
}
putchar(x%10|48);
}
struct node{
int val,cnt,size;
node *lc,*rc;
};
node *root;
int n,x;
void insert(int x,node *root){
root->size++;
if(root->val==x){
root->cnt++;
return ;
}
if(root->val>x){
if(root->lc!=NULL){
insert(x,root->lc);
}
else{
root->lc=new(node);
root=root->lc;
root->val=x;
root->size=1;
root->cnt=1;
return ;
}
}
else{
if(root->rc!=NULL){
insert(x,root->rc);
}
else{
root->rc=new(node);
root=root->rc;
root->val=x;
root->size=1;
root->cnt=1;
return ;
}
}
}
void dfs(node *root){
if(root==NULL){
return ;
}
dfs(root->lc);
for(int i=1;i<=root->cnt;i++){
write(root->val);
putchar(' ');
}
dfs(root->rc);
}
int main(){
std::ios::sync_with_stdio(false);
n=read();
for(int k=1;k<=n;k++){
x=read();
if(root==NULL){
root=new(node);
root->val=x;
root->size=1;
root->cnt=1;
continue;
}
insert(x,root);
}
dfs(root);
return 0;
}