Binary tree reconstruction uva 536

#include<iostream>
#include<cstring>
using namespace std;
char pre_order[27];
char in_order[27];
void build(int l1, int r1, int l2, int r2, int p) {
    if(l1 > r1) return ;
    for( p = l2; in_order[p] != pre_order[l1]; p++);
    build( l1 + 1, l1 + p - l2,  l2, p - 1,p);
    build( l1 + p - l2 + 1, r1, p + 1, r2, p);//The two sentences are recursive, output its left child
    cout << in_order[p];
}
int main() {
    while( cin >> pre_order >> in_order) {//1. Streaming input is like water flow. If it is broken (space, newline), it will stop. 2. When cin inputs an array, it will be automatically placed one by one.
        int l = strlen(pre_order) - 1;
        build(0, l, 0, l, 0);
        cout << endl;
    }
    return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325639137&siteId=291194637