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;
}


发布了44 篇原创文章 · 获赞 9 · 访问量 4万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览