PAT (Advanced Level) 1086. Tree Traversals Again (25)

入栈顺序为先序遍历,出栈顺序为中序遍历。

#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<stack>
using namespace std;

const int maxn=1000+10;
const int INF=0x7FFFFFFF;
int n,tot;
int Preorder[maxn],Inorder[maxn],Postorder[maxn];
int APreorder[maxn],AInorder[maxn];
int ans[maxn];
struct Binary_Tree
{
    int left;
    int right;
    int date;
} node[maxn];
int p1,p2;
stack<int>s;

void Build_Binary_Tree(int L,int R,int father)
{
    int i,MIN=INF,MAX=-INF;
    for(i=L; i<=R; i++)
    {
        if(APreorder[Inorder[i]]>MAX) MAX=APreorder[Inorder[i]];
        if(APreorder[Inorder[i]]<MIN) MIN=APreorder[Inorder[i]];
    }
    node[tot].date=Preorder[MIN];
    if(father<0) node[-father].right=tot;
    if(father>0) node[father].left=tot;
    int now=tot;
    tot++;
    if(AInorder[Preorder[MIN]]-1-L>=0)
        Build_Binary_Tree(L,AInorder[Preorder[MIN]]-1,now);
    if(R-(AInorder[Preorder[MIN]]+1)>=0)
        Build_Binary_Tree(AInorder[Preorder[MIN]]+1,R,-now);
}

void dfs(int p)
{
    if(node[p].left!=-1) dfs(node[p].left);
    if(node[p].right!=-1) dfs(node[p].right);
    ans[tot]=node[p].date;
    tot++;
}

int main()
{
    while(~scanf("%d",&n))
    {
        int i;
        tot=1;
        for(i=0; i<=n; i++)
        {
            node[i].left=-1;
            node[i].right=-1;
            node[i].date=-1;
        }

        p1=p2=1;
        for(int i=1;i<=2*n;i++)
        {
            char op[10]; scanf("%s",op);
            if(op[1]==‘u‘)
            {
                int num; scanf("%d",&num);
                s.push(num);
                Preorder[p1++]=num;
            }
            else if(op[1]==‘o‘)
            {
                int top=s.top(); s.pop();
                Inorder[p2++]=top;
            }
        }

        for(i=1; i<=n; i++)  APreorder[Preorder[i]]=i;
        for(i=1; i<=n; i++ )AInorder[Inorder[i]]=i;

        Build_Binary_Tree(1,n,0);

        tot=0;
        dfs(1);
        for(i=0; i<n; i++)
        {
            if(i<n-1) printf("%d ",ans[i]);
            else printf("%d\n",ans[i]);
        }
    }
    return 0;
}
时间: 2024-10-08 12:53:44

PAT (Advanced Level) 1086. Tree Traversals Again (25)的相关文章

PAT (Advanced Level) 1020. Tree Traversals (25)

递归建树,然后BFS一下 #include<iostream> #include<cstring> #include<cmath> #include<algorithm> #include<cstdio> #include<queue> #include<vector> using namespace std; const int maxn=40; int a[maxn],b[maxn]; int n,tot; struc

Pat(Advanced Level)Practice--1086(Tree Traversals Again)

Pat1086代码 题目描述: An inorder binary tree traversal can be implemented in a non-recursive way with a stack. For example, suppose that when a 6-node binary tree (with the keys numbered from 1 to 6) is traversed, the stack operations are: push(1); push(2)

1086. Tree Traversals Again (25)【二叉树】——PAT (Advanced Level) Practise

题目信息 1086. Tree Traversals Again (25) 时间限制200 ms 内存限制65536 kB 代码长度限制16000 B An inorder binary tree traversal can be implemented in a non-recursive way with a stack. For example, suppose that when a 6-node binary tree (with the keys numbered from 1 to

PAT Advanced 1086 Tree Traversals Again (25分)

An inorder binary tree traversal can be implemented in a non-recursive way with a stack. For example, suppose that when a 6-node binary tree (with the keys numbered from 1 to 6) is traversed, the stack operations are: push(1); push(2); push(3); pop()

PAT Advanced 1086 Tree Traversals Again (25) [树的遍历]

题目 An inorder binary tree traversal can be implemented in a non-recursive way with a stack. For example, suppose that when a 6-node binary tree (with the keys numbered from 1 to 6) is traversed, the stack operations are: push(1); push(2); push(3); po

PAT 1086 Tree Traversals Again (25)

An inorder binary tree traversal can be implemented in a non-recursive way with a stack. For example, suppose that when a 6-node binary tree (with the keys numbered from 1 to 6) is traversed, the stack operations are: push(1); push(2); push(3); pop()

PAT:1086. Tree Traversals Again (25) AC

#include<stdio.h> #include<string.h> #include<stack> using namespace std; const int MAX=50; int n,cnt=0; //n个节点,cnt在后序输出的时候控制空格数量用 int PRE[MAX],IN[MAX]; //先序,中序 int preI,inI; //先序,中序的下标 stack<int> S; //栈 struct node { int data; nod

1086. Tree Traversals Again (25)

时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue push 的顺序就是二叉树的前序 pop的顺序就是二叉树的中序遍历 本质上还是考根据这两个顺序建立二叉树,并且进行后序遍历 An inorder binary tree traversal can be implemented in a non-recursive way with a stack. For example, suppose that when

PAT Advanced Level 1013 Battle Over Cities (25)(25 分)

1013 Battle Over Cities (25)(25 分) It is vitally important to have all the cities connected by highways in a war. If a city is occupied by the enemy, all the highways from/toward that city are closed. We must know immediately if we need to repair any