pat甲级1020的一种简单解法

// pat1020.cpp: 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <queue>
#include <iostream>
using namespace std;
struct node
{
    int root;
    int start;
    int end;
};
int in[31];
int post[31];
int level[31];
int n,index=0;
int main()
{
    queue<node> q;
    cin >> n;
    for (int i = 0; i < n; i++)
        cin >> post[i];
    for (int i = 0; i < n; i++)
        cin >> in[i];
    q.push({ n - 1,0,n - 1 });
    while (!q.empty())
    {
        node n= q.front();
        q.pop();
        level[index++] = post[n.root];
        int i = 0;
        while (in[i] != post[n.root]) i++;
        if (n.start < i) q.push({ n.root - 1 - n.end + i ,n.start,i - 1 });
        if (n.end > i) q.push({ n.root - 1 , i + 1,n.end});
    }
    for (int i = 0; i < index; i++)
    {
        if(i!=0)cout << " ";
        cout << level[i];
    }
    return 0;
}

 

posted @ 2020-02-13 21:24  Lithium2020  阅读(162)  评论(0)    收藏  举报