• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            Tree Recovery HEUOJ 1045 樹的遍歷

            Posted on 2012-04-26 13:32 lenohoo 閱讀(221) 評論(0)  編輯 收藏 引用

            Tree Recovery

            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

            告訴你樹的先序遍歷,中序遍歷,求其后序遍歷
            #include<cstdio>
            #include
            <cstring>
            #include
            <iostream>
            using namespace std;
            char ch1[30],ch2[30],ch[30];
            int len;
            void dfs(int s1,int e1,int s2,int e2){
                
            if(s1>e1) return;
                ch[
            --len]=ch1[s1];
                
            int i;
                
            for(i=s2;ch2[i]!=ch1[s1] && i<=e2;i++);
                dfs(s1
            +i-s2+1,e1,i+1,e2);
                dfs(s1
            +1,s1+i-s2,s2,i-1);
            }
            int main(){
                
            while(~scanf("%s%s",ch1,ch2)){
                    len
            =strlen(ch1);
                    ch[len]
            ='\0';
                    dfs(
            0,len-1,0,len-1);
                    printf(
            "%s\n",ch);
                }
            }

            posts - 3, comments - 1, trackbacks - 0, articles - 16

            Copyright © lenohoo

            久久精品国产亚洲AV不卡| 精品国产青草久久久久福利| 国产精品99久久精品| .精品久久久麻豆国产精品| 一级做a爰片久久毛片16| 久久无码一区二区三区少妇 | 亚洲精品美女久久久久99小说| 久久午夜夜伦鲁鲁片免费无码影视| 久久精品中文无码资源站| 精品国产福利久久久| 久久丫忘忧草产品| 中文精品久久久久国产网址| 久久久一本精品99久久精品88| 996久久国产精品线观看| 日韩人妻无码一区二区三区久久99 | 亚洲成色999久久网站| 久久久亚洲欧洲日产国码是AV| 久久久青草青青亚洲国产免观| 久久综合亚洲色一区二区三区| 91精品国产91久久久久久蜜臀| 日韩AV无码久久一区二区| 婷婷久久综合| 久久AⅤ人妻少妇嫩草影院| 精品永久久福利一区二区| 精品久久久中文字幕人妻| 久久亚洲欧洲国产综合| 国产午夜精品久久久久九九| 97精品国产91久久久久久| 热re99久久精品国99热| 亚洲精品美女久久久久99小说| 国产精品永久久久久久久久久| 久久国产精品一区二区| AV色综合久久天堂AV色综合在| 欧洲成人午夜精品无码区久久| 久久精品国产亚洲AV影院| 中文成人无码精品久久久不卡 | 久久婷婷五月综合97色一本一本| 久久大香萑太香蕉av| 久久久久久伊人高潮影院| 久久人妻无码中文字幕| 色综合久久无码中文字幕|