The Postorder enumeration(二叉樹遍歷:前中轉後)

【題目描述】

Give you the Preorder enumeration and Inorder enumeration of a binary tree, output its Postorder enumeration. We assume the elements of binary tree are different.

【輸入要求】

The first line contains the number of test cases, N.
In each tese case, the first line is the Preorder enumeration and the second line is Inorder enumeration.

1
ABDCEGFHI
BDAGECHFI

【輸出要求】

Out put N lines. Each line represent the Postorder enumeration

DBGEHIFCA
【我的程序】

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

int index;
string qian,zhong;

void enumeration(int x, int y)
{
    if (x>y) return;
    char root=qian[index++];
    int pos=zhong.find(root,x);
    enumeration(x,pos-1);
    enumeration(pos+1,y);
    cout<< root;
}

int main()
{
    int n;
    cin>> n;
    cin.get();
    for (int i=0;i<n;i++)
    {
        index=0;
        getline(cin,qian);
        getline(cin,zhong);
        enumeration(0,zhong.length()-1);
        cout<< endl;
    }
    return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章