POJ2255-Tree Recovery【Binary tree traversal】

Binary tree traversal
Given the preorder and inorder, find the postorder

#include <iostream>
#include<stdio.h>
#include<string.h>
using namespace std;

char mid[27];
char pre[27];
int n=-1;
void maketree(int i,int j)
{
    if(i>j) return ;
    n++;
    int k;
    for( k=i; k<=j; k++)
    {
        if(pre[n]==mid[k])
        {
            break;
        }
    }
    maketree(i,k-1);
    maketree(k+1,j);
    printf("%c",mid[k]);
}
int main()
{
    while(~scanf("%s%s",pre,mid))
    {
        maketree(0,strlen(pre)-1);
        printf("\n");
        n=-1;
    }
    return 0;
}

Guess you like

Origin blog.csdn.net/Li_Hongcheng/article/details/83246334