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

AAAAAccepted code:

 #define HAVE_STRUCT_TIMESPEC
#include<bits/stdc++.h>
using namespace std;
stack<int>st;
int a[],b[];
int ans[];
void build(int n,int*a,int*b,int*ans){
if(!n)
return ;
int x=find(b,b+n,a[])-b;
build(x,a+,b,ans);
build(n-x-,a+x+,b+x+,ans+x);
ans[n-]=a[];
}
int main(){
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n;
cin>>n;
int cnt1=,cnt2=;
for(int i=;i<=n+n;++i){
string s;
cin>>s;
int x;
if(s[]=='u')
cin>>x;
if(s[]=='u'){
a[cnt1++]=x;
st.push(x);
}
else{
b[cnt2++]=st.top();
st.pop();
}
}
build(n,a,b,ans);
for(int i=;i<n;++i){
cout<<ans[i];
if(i<n-)
cout<<" ";
}
return ;
}

【PAT甲级】1086 Tree Traversals Again (25 分)(树知二求一)的更多相关文章

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

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

  2. PAT 甲级 1086 Tree Traversals Again

    https://pintia.cn/problem-sets/994805342720868352/problems/994805380754817024 An inorder binary tree ...

  3. 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 ...

  4. PTA 03-树3 Tree Traversals Again (25分)

    题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/667 5-5 Tree Traversals Again   (25分) An inor ...

  5. 数据结构课后练习题(练习三)7-5 Tree Traversals Again (25 分)

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

  6. PAT 甲级 1020 Tree Traversals (25分)(后序中序链表建树,求层序)***重点复习

    1020 Tree Traversals (25分)   Suppose that all the keys in a binary tree are distinct positive intege ...

  7. PAT 甲级 1020 Tree Traversals (25 分)(二叉树已知后序和中序建树求层序)

    1020 Tree Traversals (25 分)   Suppose that all the keys in a binary tree are distinct positive integ ...

  8. PAT 甲级 1020 Tree Traversals (二叉树遍历)

    1020. Tree Traversals (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Suppo ...

  9. PAT甲级——1130 Infix Expression (25 分)

    1130 Infix Expression (25 分)(找规律.中序遍历) 我是先在CSDN上面发表的这篇文章https://blog.csdn.net/weixin_44385565/articl ...

随机推荐

  1. Laravel-admin 刷新页面后,左侧菜单栏怎么默认选中当前栏目

    修改public\vendor\laravel-admin\laravel-admin里面的laravel-admin.js $(window).on('load',function(){ $('.s ...

  2. C++-指针阅读能力提升

    下面的标识符你肯定在工程中看不到,但是在面试题中却非常常见,掌握掌握还是不错的. int (*p1)(int*, int (*f)(int*));              int (*p2[5])( ...

  3. codeforces 1282 E. The Cake Is a Lie (dfs+构造)

    链接:https://codeforces.com/contest/1282/problem/E 题意:给的是一张平面图,是一个n边形,每次可以切一刀,切出一个三角形,最终切成n-2个三角形.题目给出 ...

  4. java+layui的Excel导入导出

    html: <button class="layui-btn" onclick="exportData();">导出</button> ...

  5. rancher2中文文档地址

    rancher2中文文档地址 待办 https://docs.rancher.cn/

  6. ssh连不上的问题

    新安装的ubuntu,想要ssh远程连接,发现连接不上,何解? 答 : 在ubuntu上安装ssh. sudo apt-get install openssh-server

  7. Reading Comprehensive

    我是红色 When I re-entered the full-time workforce a few years ago after a decade of solitary[隐士,独居] sel ...

  8. 229. 求众数 II

    Q: 给定一个大小为 n 的数组,找出其中所有出现超过 ⌊ n/3 ⌋ 次的元素. 说明: 要求算法的时间复杂度为 O(n),空间复杂度为 O(1). 示例 1: 输入: [3,2,3] 输出: [3 ...

  9. DVWA全级别之Brute Force(暴力破解)

    Brute Force Brute Force,即暴力(破解),是指黑客利用密码字典,使用穷举法猜解出用户口令. 首先我们登录DVWA(admin,password),之后我们看网络是否为无代理,: ...

  10. [Reversal 剧情设计] 序言

    想开发一个图形游戏已经很久了,这个寒假好不容易学了一些OpenGL知识,初步具备了开发图形游戏的能力,我觉得我可以开始了.当然,我的游戏并不是冷冰冰的PVE,肯定还需要一些剧情丰富色彩. 本来我的游戏 ...