zoukankan      html  css  js  c++  java
  • Tree Recovery(由先、中序列构建二叉树)

    题目来源:

    http://poj.org/problem?id=2255

    题目描述:

    Description

    Little Valentine liked playing with binary trees very much. Her favorite game was constructing randomly looking binary trees with capital letters in the nodes.
    This is an example of one of her creations:

    D
    /
    /
    B E
    /
    /
    A C G
    /
    /
    F

    To record her trees for future generations, she wrote down two strings for each tree: a preorder traversal (root, left subtree, right subtree) and an inorder traversal (left subtree, root, right subtree). For the tree drawn above the preorder traversal is DBACEGF and the inorder traversal is ABCDEFG.
    She thought that such a pair of strings would give enough information to reconstruct the tree later (but she never tried it).

    Now, years later, looking again at the strings, she realized that reconstructing the trees was indeed possible, but only because she never had used the same letter twice in the same tree.
    However, doing the reconstruction by hand, soon turned out to be tedious.
    So now she asks you to write a program that does the job for her!

    Input

    The input will contain one or more test cases.
    Each test case consists of one line containing two strings preord and inord, representing the preorder traversal and inorder traversal of a binary tree. Both strings consist of unique capital letters. (Thus they are not longer than 26 characters.)
    Input is terminated by end of file.

    Output

    For each test case, recover Valentine's binary tree and print one line containing the tree's postorder traversal (left subtree, right subtree, root).

    Sample Input

    DBACEGF ABCDEFG
    BCAD CBAD
    

    Sample Output

    ACBFGED
    CDAB
    
    解题思路:
    根据给出的先根序列和中根序列构建二叉树后,后序遍历二叉树即可。
    拿第一个样例来说,现在先根序列中找到根节点D,然后在中根序列中找到D,可以得到以D为根节点的二叉树的左子树有ABC,以D为根节点的二叉树的右子树有EFG。接下来在先根序列中找到B,再在中根序列中将以B为根节点的二叉树
    的左右子树找到,如此递归,直至将序列处理完毕。
    代码实现:
     1 #include<stdio.h>
     2 #include<stdlib.h>
     3 #include<string.h>
     4 struct node{
     5     char ch;
     6     struct node *left,*right;
     7 };
     8 struct node* creat(char *pre,char *in,int len); 
     9 void post(struct node* head);
    10 int main()
    11 {
    12     char pre[30],in[30];
    13     struct node *head;
    14     head = ( struct node *)malloc(sizeof(struct node));
    15     while(scanf("%s %s",pre,in) != EOF)
    16     {
    17         int len=strlen(pre);
    18         head=creat(pre,in,len);
    19         
    20         post(head);//后序遍历
    21         printf("
    ");
    22     }
    23     return 0;
    24 }
    25 struct node* creat(char *pre,char *in,int len)
    26 {
    27     if(len==0)
    28         return NULL;
    29         
    30     struct node *head;
    31     head = (struct node*)malloc(sizeof(struct node));    
    32     head->ch=pre[0];
    33     
    34     char *p;
    35     for(p=in;p != NULL;p++)//指针字符串中空为结束标志 
    36         if(*p == *pre)
    37             break;
    38             
    39     int k=p-in;
    40     head->left=creat(pre+1,in,k);
    41     head->right=creat(pre+k+1,p+1,len-k-1);
    42     return head; 
    43 }
    44 void post(struct node* head)
    45 {
    46     if(head == NULL)
    47         return;
    48     post(head->left);
    49     post(head->right);
    50     printf("%c",head->ch);
    51 } 

    易错分析:

    注意注释,指针字符串和字符串数组还是有一定区别的,比如结束标志位NULL

  • 相关阅读:
    2015.2.27 UltraEdit中显示XML结构
    2015.1.31 DataGridView自动滚动到某行
    2015.1.15 利用函数实现将一行记录拆分成多行记录 (多年想要的效果)
    2015.1.15 利用Oracle函数返回表结果 重大技术进步!
    2015.1.15 利用Oracle函数插入表结构 Bulk collect into 不用循环,简洁高效
    2015.1.8 Left join 左连接
    2015.1.10 解决DataGridView SelectionChanged事件自动触发问题
    delphi 遍历窗口
    delphi 访问 protected 属性 哈哈
    clientdataset 读取excel 如果excel 文件不存在的时候 相应的gird 会不显示数据, 鼠标掠过 gird 格子 才会显示数据。 这是一个bug 哈哈
  • 原文地址:https://www.cnblogs.com/wenzhixin/p/8343860.html
Copyright © 2011-2022 走看看