• <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 閱讀(217) 評論(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

            99久久国产综合精品女同图片| 久久国产亚洲精品麻豆| 久久免费视频6| 欧美精品国产综合久久| 久久66热人妻偷产精品9| 久久精品国产精品国产精品污 | 免费一级做a爰片久久毛片潮| 一级女性全黄久久生活片免费 | 国产精品久久久久久久午夜片| 久久精品国产亚洲一区二区三区 | 91精品国产综合久久四虎久久无码一级| 91久久香蕉国产熟女线看| 久久久久亚洲AV成人网人人网站 | 狠狠精品久久久无码中文字幕| 久久精品国产99国产精品亚洲| 久久精品一区二区| 久久久久久伊人高潮影院| 成人精品一区二区久久| 亚洲日韩中文无码久久| 亚洲午夜精品久久久久久浪潮| 97精品伊人久久久大香线蕉| 欧美精品久久久久久久自慰| 一级女性全黄久久生活片免费 | 99久久国产主播综合精品| 久久亚洲AV成人无码国产| 欧美一区二区久久精品| 久久久久女教师免费一区| 亚洲欧美日韩精品久久| 久久综合久久综合九色| 久久99国产精品尤物| 天堂久久天堂AV色综合| 国内精品九九久久精品| 久久久久亚洲av综合波多野结衣 | 国产精品无码久久久久久| 亚洲AV无码久久精品狠狠爱浪潮| 久久国产亚洲精品| 亚洲精品综合久久| 久久精品中文字幕大胸| 伊人伊成久久人综合网777| 久久福利资源国产精品999| 超级97碰碰碰碰久久久久最新|