ZOJ 1700 Falling Leaves

考察二叉查找树的题目,题目用了大量篇幅来描述bst的内容,可以忽略。

 

给出每次去掉的叶子节点,然后构造bst,前序遍历输出,不用考虑倒序添加叶子时会不会重叠,直接建立节点就可以了

 

这次的代码我感觉还是写的不错的。。

 

#include<stdio.h> #include<stdlib.h> #include<string.h> struct node { char data; struct node *left,*right; }*Head; void insert(char c,struct node * & Head) { if( Head == NULL ) { Head = (struct node*)malloc(sizeof(struct node)); Head->data = c; Head->left = Head->right = NULL; return ; } if( c > Head->data ) insert(c,Head->right); else insert(c,Head->left); } void out( struct node* &Head ) { if( Head == NULL ) return ; putchar( Head->data ); out( Head->left ); out( Head->right ); } int cases(void) { char s[100]; int i,t; gets(s); if( s[0] == '</p> ) return 0; if( s[0] == '*' ) return 1; t = cases(); for( i = 0; s[i] != '/0'; i++ ) insert( s[i],Head ); return t; } int main(void) { Head = NULL; while( cases() ) out( Head ) ,printf("/n"), Head = NULL; out( Head ); return 0; }  

你可能感兴趣的:(c,struct,null,insert)