#include <stdio.h>
#include<stdlib.h>
struct node{
struct node *left;
struct node *right;
char data;
};
struct node* searchd(struct node *ptr,char data){
if(ptr->data==data) return ptr;
else if(ptr->data>data) return searchd(ptr->left,data);
else if(ptr->data<data) return searchd(ptr->right,data);
}
void ulr(struct node *ptr){
if(ptr==NULL) return;
printf("%c",ptr->data);
ulr(ptr->left);
ulr(ptr->right);
}
void lur(struct node *ptr){
if(ptr==NULL) return;
lur(ptr->left);
printf("%c",ptr->data);
lur(ptr->right);
}
void lru(struct node *ptr){
if(ptr==NULL) return;
ulr(ptr->left);
ulr(ptr->right);
printf("%c",ptr->data);
}
void editn(struct node *ptr,char ld,char rd){
if(ld!='.'){
struct node *new=(struct node*)malloc(sizeof(struct node));
new->data=ld;
ptr->left=new;
}
if(rd!='.'){
struct node *new=(struct node*)malloc(sizeof(struct node));
new->data=rd;
ptr->right=new;
}
}
int main() {
int n;
scanf("%d",&n);
struct node *ROOT=(struct node*)malloc(sizeof(struct node));
ROOT->data='A';
struct node *ptr;
for(int t=0;t<n;t++){
char k,l,r;
scanf("%c %c %c",&k,&l,&r);
ptr=searchd(ROOT,k);
if(ptr!=NULL) editn(ptr,l,r);
}
ulr(ROOT);
lur(ROOT);
lru(ROOT);
return 0;
}
백준 1991번 풀고있고
여기저기 지우고 뭐하고 해보니까 segmentation fault가 searchd하면서 어디서 오류나서 나오는거 같은데 어디인지를 모르겠네요