PAT (Advanced Level) 1086. Tree Traversals Again (25)
入栈顺序为先序遍历,出栈顺序为中序遍历。
#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)的更多相关文章
- PAT (Advanced Level) 1020. Tree Traversals (25)
递归建树,然后BFS一下 #include<iostream> #include<cstring> #include<cmath> #include<algo ...
- 【PAT甲级】1086 Tree Traversals Again (25 分)(树知二求一)
题意:输入一个正整数N(<=30),接着输入2*N行表示栈的出入(入栈顺序表示了二叉搜索树的先序序列,出栈顺序表示了二叉搜索树的中序序列),输出后序序列. AAAAAccepted code: ...
- PAT 甲级 1086 Tree Traversals Again (25分)(先序中序链表建树,求后序)***重点复习
1086 Tree Traversals Again (25分) An inorder binary tree traversal can be implemented in a non-recu ...
- PTA (Advanced Level) 1020 Tree Traversals
Tree Traversals Suppose that all the keys in a binary tree are distinct positive integers. Given the ...
- 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 ...
- 1086. Tree Traversals Again (25)
题目如下: An inorder binary tree traversal can be implemented in a non-recursive way with a stack. For e ...
- PAT (Advanced Level) 1113. Integer Set Partition (25)
简单题. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #in ...
- PAT (Advanced Level) 1094. The Largest Generation (25)
简单DFS. #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> ...
- PAT (Advanced Level) 1074. Reversing Linked List (25)
简单题. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #in ...
随机推荐
- rf统计条数
js模式 直接引用关键字模式
- 用border实现三角形的过程
div{ width:100px; height:100px; background:yellow; border-top: 20px solid red; border-right:20px sol ...
- linux——nmap端口扫描命令
先安装 nmap :apt-get install nmap 端口扫描命令nmap -sS 172.16.55.100nmap -Pn 172.16.55.100第一组渗透测试指令,用于情报收集. 要 ...
- XML解析(一) DOM解析
XML解析技术主要有三种: (1)DOM(Document Object Model)文档对象模型:是 W3C 组织推荐的解析XML 的一种方式,即官方的XML解析技术. (2)SAX(Simple ...
- 二. python函数与模块
第四章.内置函数与装饰器详解 1.内置函数补充1 注:红色圆圈:必会: 紫红色方框:熟练: 绿色:了解 callable() 判断函数是否可以被调用执行 def f1(): pass f1() ...
- Git学习之路
目录 git安装 linux windows git命令 创建版本库 提交文件 仓库状态 版本回退 工作区和暂存区 工作区 暂存区 推送.下拉和克隆 推送 下拉 克隆 git应该可以说是程序员必备技能 ...
- MySQL丨01丨数据库基本概念
以前记录数据可能很少也很简单,比如说老王借了老李半斤肉,这样的数据老李直接就写到墙上就行了. 后来数据多了人们就以表格的方式开始记录,写到一张A4纸上,比如学生的档案,有表头和序号等. 表头里有姓名. ...
- perl学习之子例程
1.system function && user function system fucntion:chomp reverse print... user function: & ...
- Python数据分析库之pandas,你该这么学!No.1
写这个系列背后的故事 咦,面试系列的把基础部分都写完啦,哈哈答,接下来要弄啥嘞~ pandas吧 外国人开发的 翻译成汉语叫 熊猫 厉害厉害,很接地气 一个基于numpy的库 干啥的? 做数据分析用的 ...
- 电脑连接到手机并安装手机驱动usb-driver
设置真机开发环境需要执行下面几个步骤: 1.在应用的Manifest文件中声明应用是可调试的: 2.打开应用的调试支持: 对于通过Eclipse创建的应用,可以省略步骤2,因为在Eclipse IDE ...