1. 程式人生 > >知先根、中根求後根

知先根、中根求後根

原理和上篇文章相似,不贅述

#include <iostream>
#include <cstring>
using namespace  std;

void f(char s1[], char s2[], int len)
{
    if (len == 1)
    {
        cout << s1[0];
    }
    else if (len > 1)
    {
        char* index = strchr(s2, s1[0]);
        int new_len = (int)(index - s2);
        
        f(s1 + 1, s2, new_len);
        f(s1 + new_len + 1, index + 1, len - new_len - 1);
        cout << s1[0];
    }
}

int main()
{
    char s1[100], s2[100];
    
    //s1先序, s2中序
    cin >> s1 >> s2;
    
    f(s1, s2, (int)strlen(s1));
    
    return 0;
}