-
Title Description:
-
A program that reads a string of first-order traversal strings of user input and establishes a binary tree (stored as a pointer) based on this string.
For example, the following is an ordinal traversal string:
abc# #DE #g# #F # # #
Where "#" represents a space, an empty characters represents an empty tree. After this binary tree is established, the sequential traversal of the two-fork tree is performed, and the output traversal results are obtained.
-
Input:
-
The input includes 1 lines of string, with a length of not more than 100.
-
Output:
-
There may be multiple sets of test data, for each set of data,
The output sets the input string to the sequence traversed by the two-forked tree, followed by a space after each character.
One row for each output result.
-
Sample input:
-
abc# #de #g# #f # # #
-
-
Sample output:
-
#include <stdio.h>#include<stdlib.h>Charcache[101];typedefstructnode{Chardata; structNode *lchild,*Rchild;} Tree;tree*Root;intCnt;tree*Build_tree () {Tree*Root; if(cache[cnt++]=='#') root=NULL; Else{root= (tree *)malloc(sizeof(tree)); Root->data = cache[cnt-1]; Root->lchild =Build_tree (); Root->rchild =Build_tree (); } returnRoot;}voidIn_order (Tree *root) { if(root!=NULL) {In_order (Root-lchild); printf ("%c",root->data); In_order (Root-rchild); }}intMain () { while(~SCANF ("%s", Cache)) {CNT=0; Root=Build_tree (); In_order (root); }}
Nine degree OJ1184 two fork tree