当前位置:网站首页>7-2 construction of binary tree by post order + middle order sequence

7-2 construction of binary tree by post order + middle order sequence

2022-06-24 23:31:00 White -

7-2 In the following order + Construction of binary tree by middle order sequence

In the following order + Construction of binary tree by middle order sequence

sample input :
First line input sequence length n, Second line input n Characters represent the sequence of post order traversal of binary tree , In the third line, enter n Characters represent the sequence of ordinal traversal in a binary tree

9
GHDBEIFCA
GDHBAECIF

sample output :
Output binary tree preorder traversal sequence .

ABDGHCEFI

Code :

#include <stdio.h>
#include <stdlib.h>
int n;
void CreatTree(int n,char after[],char mid[])
{
    
    if(n<0)
        return ;
    else
    {
    
        printf("%c",after[n]);
        int i;
        for(i=0;i<=n;i++)
        {
    
            if(mid[i]==after[n])
                break;
        }
        CreatTree(i-1,after,mid);
        CreatTree(n-i-1,after+i,mid+i+1);
        return ;
    }
}
int main()
{
    
    char after[1000];
    char mid[1000];
    scanf("%d",&n);
    scanf("%s",after);
    scanf("%s",mid);
    CreatTree(n-1,after,mid);
    return 0;
}

202206222111 3、 ... and

原网站

版权声明
本文为[White -]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/175/202206241820370794.html