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

#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. python Object-Oriented Programming

    Python 类的成员.成员修饰符.类的特殊成员. Python 类的成员 类的成员可以分为三大类: 字段.方法和属性. #注:所有成员中,只有普通字段的内容保存对象中,即: #根据此类创建了多少对象 ...

  2. 14.list列表

    1).列表的切片 li = ['德玛西亚',[1,2,3],'luokesasi','eson','女神','jingdongi'] l1 = li[0] print(l1) #>>> ...

  3. javascript的offset、client、scroll使用方法

    offsetTop 指元素距离上方或上层控件的位置,整型,单位像素. offsetLeft 指元素距离左方或上层控件的位置,整型,单位像素. offsetWidth 指元素控件自身的宽度,整型,单位像 ...

  4. 关于Java IO流学习总结

    一.IO流的三种分类方式 1.按流的方向分为:输入流和输出流 2.按流的数据单位不同分为:字节流和字符流 3.按流的功能不同分为:节点流和处理流     二.IO流的四大抽象类: 字符流:Reader ...

  5. Linux系统状态检测

    基于Red Hat Enterprise Linux 7.5 1.ifconfig ifconfig用于获取和配置网络接口的网络参数,格式为“ifconfig [网络设备] [参数]” 参数: add ...

  6. 初中级PHP面试基础汇总

    这是我整理的一套面试题,老铁们看看就当复习了哦 相关PHP面试题 搞定PHP面试 - 函数知识点整理 php 面试题目整理 PHP面试整理 PHP面试 概述 感觉现在发面试题有些冷门,就跟昨天德国那场 ...

  7. React碰到v-if

    最近在重构公司老项目,由于本人以前的技术栈是vue, 换工作后发现现在公司的技术栈是react, 所以重构的过程是及其痛苦.加之项目又是几年前的老项目,不敢大改,比葫芦画瓢比比皆是.本文就介绍下遇到的 ...

  8. python 购物车小程序(列表、循环、条件语句)

    goods = [ ['iphone6s', 5800], ['mac book', 9000], ['coffee', 32], ['python book', 80], ['bicyle', 15 ...

  9. leetcode-3-basic-divide and conquer

    解题思路: 因为这个矩阵是有序的,所以从右上角开始查找.这样的话,如果target比matrix[row][col]小,那么就向左查找:如果比它大,就 向下查找.如果相等就找到了,如果碰到边界,就说明 ...

  10. 【MySQL】浅谈 varchar(N)

    一  前言  varchar(N) N代表什么意思,能存放多少个中文字符?属于老生常谈的问题了,今天又被一个开发同事问我关于这个问题,索性写一篇文章来具体介绍一下.二 理论知识 先说明一下 MySQL ...