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;
    }        
    

      

  • 相关阅读:
    【java】一种自动生成数据库文档的方法
    sublime vintage mode 按住一个键(比如 j)不会重复的问题
    SQL Service服务更换帐户后无法启动的情况(Security 4625 Type 5)
    隐藏的计划任务运行,导致账户被锁的调查方法
    nodejs 热更新页面
    数组转tree arrToTree
    compose函数
    react 类组件的生命周期
    qiankun 微应用demo
    application 'xxx' died in status LOADING_SOURCE_CODE: [qiankun] You need to export lifecycle functions in xxx entry
  • 原文地址:https://www.cnblogs.com/zhangliu/p/7052724.html
Copyright © 2011-2022 走看看