-
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:
-
-
-
Source:
-
- 2002 Huazhong University of Science and technology computer research Vitality Test real problem ,
-
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
typedef struct NODE
{
char m;
struct node *lchild;
struct node *rchild;
}*linklist;
void Chu (linklist *head)
{
(*head) =null;
}
void XTree (linklist *head)
{
char x;
scanf ("%c", &x);
if (x== ' # ') (*head) =null;
Else
{
(*head) = (linklist) malloc (sizeof (struct node));
(*head)->m=x;
XTree (& (*head)->lchild);
XTree (& (*head)->rchild);
}
}
void Ztree (linklist head)
{
if (head)
{
Ztree (Head->lchild);
printf ("%c", head->m);
Ztree (Head->rchild);
}
}
int main ()
{
Char s[100];
int n;
Linklist head;
Chu (&head);
scanf ("%d", &n);
while (n--)
{
XTree (&head);
Ztree (head);
}
return 0;
}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Nine degrees OJ two fork Tree traversal topic 1184