二叉木、先序、中序のシーケンスに基づいて、後続の遍歴結果(c+)を求める
二叉木、先序、中序のシーケンスに基づいて、後続の遍歴結果を求めます
入力:
出力:
サンプル入力:
サンプル出力:
完全なコード:
入力:
。
。
出力:
サンプル入力:
426315
623415
サンプル出力:
632514
完全なコード:
#include<bits/stdc++.h>
using namespace std;
char pre[100];
char hou[100];
//
typedef struct node{
char data;
struct node *lc,*rc;
} *BTree;
// 、
BTree build(int l1, int r1, int l2, int r2){
BTree T = new node;
T->data = pre[l1];
T->lc=NULL;
T->rc=NULL;
int pos = l2;
while(hou[pos]!=pre[l1]){
pos++;
}
int cnum = pos-l2;
if(pos>l2){
T->lc = build(l1+1, l1+cnum, l2, pos-1);
}
if(pos<r2){
T->rc = build(l1+cnum+1, r1, pos+1, r2);
}
return T;
}
//
void Hou(BTree T){
if(T!=NULL){
Hou(T->lc);
Hou(T->rc);
cout<<T->data;
}
}
//
int main(){
cin>>pre;
cin>>hou;
BTree T = build(0, strlen(pre)-1, 0, strlen(hou)-1);
Hou(T);
}