Nine degrees OJ two fork Tree traversal topic 1184

Source: Internet
Author: User


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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.