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

#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<stack>
using namespace std; const int maxn=+;
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<) node[-father].right=tot;
if(father>) node[father].left=tot;
int now=tot;
tot++;
if(AInorder[Preorder[MIN]]--L>=)
Build_Binary_Tree(L,AInorder[Preorder[MIN]]-,now);
if(R-(AInorder[Preorder[MIN]]+)>=)
Build_Binary_Tree(AInorder[Preorder[MIN]]+,R,-now);
} void dfs(int p)
{
if(node[p].left!=-) dfs(node[p].left);
if(node[p].right!=-) dfs(node[p].right);
ans[tot]=node[p].date;
tot++;
} int main()
{
while(~scanf("%d",&n))
{
int i;
tot=;
for(i=; i<=n; i++)
{
node[i].left=-;
node[i].right=-;
node[i].date=-;
} p1=p2=;
for(int i=;i<=*n;i++)
{
char op[]; scanf("%s",op);
if(op[]=='u')
{
int num; scanf("%d",&num);
s.push(num);
Preorder[p1++]=num;
}
else if(op[]=='o')
{
int top=s.top(); s.pop();
Inorder[p2++]=top;
}
} for(i=; i<=n; i++) APreorder[Preorder[i]]=i;
for(i=; i<=n; i++ )AInorder[Inorder[i]]=i; Build_Binary_Tree(,n,); tot=;
dfs();
for(i=; i<n; i++)
{
if(i<n-) printf("%d ",ans[i]);
else printf("%d\n",ans[i]);
}
}
return ;
}

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

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

    递归建树,然后BFS一下 #include<iostream> #include<cstring> #include<cmath> #include<algo ...

  2. 【PAT甲级】1086 Tree Traversals Again (25 分)(树知二求一)

    题意:输入一个正整数N(<=30),接着输入2*N行表示栈的出入(入栈顺序表示了二叉搜索树的先序序列,出栈顺序表示了二叉搜索树的中序序列),输出后序序列. AAAAAccepted code: ...

  3. PAT 甲级 1086 Tree Traversals Again (25分)(先序中序链表建树,求后序)***重点复习

    1086 Tree Traversals Again (25分)   An inorder binary tree traversal can be implemented in a non-recu ...

  4. PTA (Advanced Level) 1020 Tree Traversals

    Tree Traversals Suppose that all the keys in a binary tree are distinct positive integers. Given the ...

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

    题目 An inorder binary tree traversal can be implemented in a non-recursive way with a stack. For exam ...

  6. 1086. Tree Traversals Again (25)

    题目如下: An inorder binary tree traversal can be implemented in a non-recursive way with a stack. For e ...

  7. PAT (Advanced Level) 1113. Integer Set Partition (25)

    简单题. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #in ...

  8. PAT (Advanced Level) 1094. The Largest Generation (25)

    简单DFS. #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> ...

  9. PAT (Advanced Level) 1074. Reversing Linked List (25)

    简单题. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #in ...

随机推荐

  1. Linux内核漏洞利用-环境配置(转)

    实验环境: Ubuntu-14.04.1 x86 linux-2.6.32.1 busybox-1.27.2 qemu 0x00 安装qemu sudo apt-get install qemu qe ...

  2. swift-通知的基本使用

    swift-通知的基本使用   //通知的使用 1.发通知.(以这条通知为例,通知名字:nickNameNotification 通知参数:title) NSNotificationCenter.de ...

  3. 调用 C 动态库

    调用 C 动态库 由 王巍 (@ONEVCAT) 发布于 2015/11/04 C 是程序世界的宝库,在我们面向的设备系统中,也内置了大量的 C 动态库帮助我们完成各种任务.比如涉及到压缩的话我们很可 ...

  4. noip_最后一遍_1-数学部分

    它就是要来了 noip数论一般会以三种形式呈现 注 码风可能有些毒 (有人说我压行qwq) 大概保持标准三十五行左右 为什么是三十五行呢 因为我喜欢这个数字 我喜欢三十五而已(足球球衣也会用这个号哒) ...

  5. inotify+rsync sersync+rsync实时同步服务

    中小型网站搭建-数据实时的复制-inotify/sersync inotify是一种强大的,细粒度的.异步的文件系统事件监控机制(软件),linux内核从2.6.13起,加入inotify支持,通过i ...

  6. 【cookie】【浏览器】各大浏览器对cookie的限制

  7. python中strip(),lstrip(),rstrip()函数的讲解

    1. strip() 它的函数原型:string.strip(s[, chars]),它返回的是字符串的副本,并删除前导和后缀字符.(意思就是你想去掉字符串里面的哪些字符,那么你就把这些字符当参数传入 ...

  8. 关于EF使用脏读(连接会话开始执行设置隔离级别)

    SQL Server中事物隔离级别Read Uncommitted和with(nolock) 注意:应该使用后者(修改后的版本):

  9. Python基础之(判断,循环,列表,字典)

    一.python介绍 Python是一种简单易学,功能强大的编程语言,它有高效率的高层数据结构,简单而有效地实现面向对象编程.Python简洁的语法和对动态输入的支持,再加上解释性语言的本质,使得它在 ...

  10. 为什么我打的jar包没有注解?

    本文来自网易云社区 作者:王飞 一.前言 一切问题的起源就是来自一个问题"为什么我打的jar包没有注解?",带着这个疑问查了一圈资料,原来问题主要是在没有将源码中的注释进行抽取打包 ...