#include <stdio.h>
#include <stdlib.h>
typedef struct tree{
struct tree* lchild;
struct tree* rchild;
int data;
} Tree, *ptr;
ptr createTree(){
ptr p = malloc(sizeof(Tree));
p->lchild = NULL;
p->rchild = NULL;
return p;
}
ptr insert(ptr p, int temp){
if(p==NULL) {p = createTree();p->data=temp;}
else{
if(p->data > temp) p->lchild = insert(p->lchild, temp);
else if(p->data < temp) p->rchild = insert(p->rchild, temp);
}
return p;
}
void visit(ptr p){
if(p != NULL){
printf("%d,",p->data);
visit(p->lchild);
visit(p->rchild);
}
}
int main(){
ptr head=NULL;
char c;
int num;
while(scanf("%d,",&num) != EOF){
head = insert(head, num);
}
visit(head);
}
最不安神晴又复雨
于 2022-10-16 21:50:06 首次发布