#include #include #include struct tree { char c; struct tree * left; struct tree * right; }; void free_tree (struct tree * t) { if (!t) return; free_tree (t->left); free_tree (t->right); free (t); } struct tree * read_tree () { char c; struct tree * t; int ret; ret = scanf ("%c", &c); assert (c == '('); ret = scanf ("%c", &c); assert (ret == 1); if (c == ')') return NULL; t = calloc (1, sizeof (struct tree)); t->c = c; t->left = t->right = NULL; ret = (scanf ("%c", &c)); assert (ret == 1); if (c == ')') return t; assert (c == ','); t->left = read_tree (); ret = (scanf ("%c", &c)); assert (ret == 1); if (c == ')') return t; assert (c == ','); t->right = read_tree (); ret = scanf ("%c", &c); assert (ret == 1); assert (c == ')'); return t; } void inorder (struct tree * t) { if (!t) return; inorder (t->left); printf ("%c", t->c); inorder (t->right); } void postorder (struct tree * t) { if (!t) return; postorder (t->left); postorder (t->right); printf ("%c", t->c); } int main () { struct tree * t; while (1) { scanf (" "); if (feof (stdin)) break; t = read_tree (); inorder (t); printf ("\n"); postorder (t); printf ("\n"); free_tree (t); } return 0; }