Data structure Experiment two forks tree two: traverse the binary tree time limit:1000ms Memory limit:65536kbsubmit Statisticproblem Description
A sequence of characters of a binary tree that is known to iterate through the input, such as a abc,,de,g,,f,,, (which represents an empty node). Build a two-fork tree and traverse the two-tree in both the middle and post order ways.
Input
Enter multiple sets of data consecutively, with each set of data entering a string of less than 50 characters in length.
Output
Each set of input data corresponds to output 2 lines:
The 1th line outputs the sequential traversal sequence;
The 2nd line outputs the sequential traversal sequence.
Example Input
Abc,,de,g,,f,,,
Example Output
Cbegdfacgefdba
DQE
According to the complete sequence of binary tree to create a two-tree and in accordance with the sequential output of the sequence, the use of simple recursion can be completed, the subject is purely a water problem, identification completed.
Attached code:
1#include <iostream>2#include <cstdio>3 4 using namespacestd;5 6 structTree7 {8 CharC;9Tree *lt,*RT;Ten }; One ATree *creat (Char*&xx) - { - if(*xx==' /') the returnNULL; - if(*xx==',') - { -xx++; + returnNULL; - } +Tree *r=NewTree; Ar->c=*xx++; atr->lt=creat (xx); -r->rt=creat (xx); - returnR; - } - - voidMout (Tree *R) in { - if(r==NULL) to return ; +Mout (r->LT); -printf"%c",r->c); theMout (r->RT); * } $ Panax Notoginseng voidHout (Tree *R) - { the if(r==NULL) + return ; AHout (r->LT); theHout (r->RT); +printf"%c",r->c); - } $ $ intMain () - { -Tree *Root; the Charxx[ -],*p; - while(SCANF ("%s", XX)! =EOF)Wuyi { thep=xx; -root=creat (p); Wu mout (root); -printf"\ n"); About Hout (root); $printf"\ n"); - } - return 0; - } A + /*************************************************** the User Name: * * * - result:accepted $ Take time:0ms the Take memory:156kb the Submit time:2016-11-03 17:44:34 the ****************************************************/
Sdut 3341 Data structure Experiment two forks tree two: traverse the binary tree