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 ...
随机推荐
- C语言中函数参数传递
C语言中函数参数传递的三种方式 (1)值传递,就是把你的变量的值传递给函数的形式参数,实际就是用变量的值来新生成一个形式参数,因而在函数里对形参的改变不会影响到函数外的变量的值.(2)地址传递,就是把 ...
- http post get 同步异步
下面首先介绍一下一些基本的概念---同步请求,异步请求,GET请求,POST请求. 1.同步请求从因特网请求数据,一旦发送同步请求,程序将停止用户交互,直至服务器返回数据完成,才可以进行下一步操作.也 ...
- webAssmebly实现js数组排序 使用custom elements和Shadow DOM自定义组件
直接上码了……………… .wat源码 (module (type $t0 (func (param i32 i32))) (type $t1 (func (result i32))) (type $t ...
- (14)zabbix Simple checks基本检测
1. 开始 Simple checks通常用来检查远程未安装代理或者客户端的服务 使用simple checks,被监控客户端无需安装zabbix agent客户端,zabbix server直接使用 ...
- DNS服务-主从架构搭建
为了网站的可靠性,通常都会有多个DNS服务器,万一DNS服务器宕机了,可以实现DNS服务器容错 通常都会有一个主DNS服务器,后面配若干个辅助DNS服务器,这个主DNS服务器的数据库会同步给其他的DN ...
- 命令行发送UDP
https://www.cnblogs.com/Dennis-mi/articles/6866762.html: 如果往本地UDP端口發送數據,那麼可以使用以下命令:echo “hello” > ...
- perl学习之内置变量
Perl内置特殊变量 一.正则表达式特殊变量:1.$n :包含上次模式匹配的第n个子串2.$& :前一次成功模式匹配的字符串3.$` :前次匹配成功的子串之前的内容4.$’ :前次匹配 ...
- 【转发】【linux】【php】centos 编译php常见错误
configure: error: xml2-config not found. Please check your libxml2 installation. yum install libxml2 ...
- 快照、克隆,xshell优化,Linux历史
目录 一.虚拟拍照功能 二.虚拟机克隆功能 三.Xshell的优化 四.介绍Linux历史 一.虚拟拍照功能 1.拍摄快照 关机状态拍照 关机命令:shutdown -h now 或者 init 0 ...
- 检查DLL,EXE文件是64位或者32位的方法
检查DLL,EXE文件是64位或者32位:输入corflags <assembly path>: