ZOJ1700 Falling Leaves
题目链接。
分析:
题目一开始说了一大堆,以为没用,就没看。然后题不会做。后来把题目仔细看了一遍。。恍然大悟。
这题的树好像叫二叉搜索树(表示木听过)。。
逆序见树。
#include <stdio.h> #include <stdlib.h> #include <string.h> typedef struct node{ char ch; struct node *left, *right; }node; void del(node *T){ if(T){ if(T->left) del(T->left); if(T->right) del(T->right); free(T); } } void Create(node **T, char c){ if((*T) == NULL){ (*T) = (node *)malloc(sizeof(node)); (*T)->left = (*T)->right = NULL; (*T)->ch = c; } else if(c < (*T)->ch) Create(&(*T)->left, c); else Create(&(*T)->right, c); } void TraversePre(node *T){ if(T){ printf("%c", T->ch); TraversePre(T->left); TraversePre(T->right); } } int main(){ char s[1000][100]; int i, len, top=0, j; node *root = NULL; while(scanf("%s", s[top++]) == 1){ if(s[top-1][0] == '*' || s[top-1][0] == '$'){ for(i=top-2; i>=0; i--){ len = strlen(s[i]); for(j=0; j<len; j++){ Create(&root, s[i][j]); } } top = 0; TraversePre(root); putchar('\n'); free(root); root = NULL; if(s[top-1][0] == '$') break; } } return 0; }