二叉树遍历 华中科技大学计算机历年考研复试上机题

#include<bits/stdc++.h>
using namespace std;
struct Node{
    Node *lchild;
    Node *rchild;
    char c;
}Tree[50];
int loc;
Node *create(){
    Tree[loc].lchild=Tree[loc].rchild=NULL;
    return &Tree[loc++];    
}
char str1[30],str2[30];
void postOrder(Node *T){   //后序遍历 
    if(T->lchild!=NULL)
        postOrder(T->lchild);
    if(T->rchild!=NULL)
        postOrder(T->rchild);
    printf("%c",T->c);
}
Node *build(int s1,int e1,int s2,int e2){
    Node* ret=create();
    ret->c=str1[s1];
    int rootIdx;
    for(int i=s2;i<=e2;i++)
        if(str2[i]==str1[s1]){
            rootIdx=i;
            break;
        }
    if(rootIdx!=s2)ret->lchild=build(s1+1,s1+(rootIdx-s2),s2,rootIdx-1);
    if(rootIdx!=e2)ret->rchild=build(s1+1+(rootIdx-s2),e1,rootIdx+1,e2);
    return ret;
}
int main(){
    while(scanf("%s",str1)!=EOF){
        scanf("%s",str2);
        loc=0;
        int L1=strlen(str1);
        int L2=strlen(str2);
        Node *T=build(0,L1-1,0,L2-1);
        postOrder(T);
        printf("\n");
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_31674679/article/details/80204151