zoukankan      html  css  js  c++  java
  • nyoj 221 Tree

    Tree

    时间限制:1000 ms  |  内存限制:65535 KB

    难度:3

    描述

    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! 

    输入

    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.

    输出

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

    样例输入

    DBACEGF ABCDEFG

    BCAD CBAD

    样例输出

    ACBFGED

    CDAB

    #include <stdio.h>
    #include <malloc.h>
    #include <string.h>
    //二叉链表
    typedef struct node{
      char data;//节点数据元素
      struct node *lchild;//指向左孩子
      struct node *rchild;//指向右孩子
    }BiNode,*BTree;
    void GetPostOrder(char *prim,char *mid,BTree &T,int len)
    {
      if(len==0)
      {
        T=NULL;
        return;
      }
      //提出先序序列中的第一个节点
      char ch=prim[0];
      int index=0;
      //在中序序列中查找当前根节点,并用index记录其在序列中的位置
      while(mid[index]!=ch)
      {
        index++;
      }
      //给根节点分配空间
      T=(BTree)malloc(sizeof(BiNode));
      T->data=mid[index];
      //建立左子树
      GetPostOrder(prim+1,mid,T->lchild,index);
      //建立右子树
      GetPostOrder(prim+index+1,mid+index+1,T->rchild,len-index-1);
    }
    //后序输出二叉树
    void PostOrder(BTree T)
    {
      if(T!=NULL)
      {
        PostOrder(T->lchild);
        PostOrder(T->rchild);
        printf("%c",T->data);
      }
    }
    int main()
    {
      char first[26],mid[26],last[26];
      while(scanf("%s%s",last,mid)!=EOF)
      {
        BTree T=NULL;
     //   GetPreOrder(last,mid,T,strlen(last));
     //   PreOrder(T);
     		GetPostOrder(last,mid,T,strlen(last));
     		PostOrder(T);
        printf("
    ");
      }
      return 0;
    }        
    

      

  • 相关阅读:
    [开源]用MQL4实现MD5加密
    如何转换WMV到MP3,WMV到MP3播放器
    C# Winform TreeView 的一些基本用法
    WinServer 2008 远程桌面连接设置
    存储数据类型的转化总结
    EF中执行sql语句,以及事务
    C#(委托a)
    LINQ绑定List到GridView
    循环遍历DataTable绑定到Table
    要将 ASP.NET 访问权限授予某个文件,请在资源管理器中右击该文件,选择“属性”,然后选择“安全”选项卡。单击“添加”添加适当的用户或组。突出显示 ASP.NET 帐户,选中所需访问权限对应的框。
  • 原文地址:https://www.cnblogs.com/zhangliu/p/7052724.html
Copyright © 2011-2022 走看看