【PAT甲级】1086 Tree Traversals Again (25 分)(树知二求一)
题意:
输入一个正整数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 分)(树知二求一)的更多相关文章
- PAT 甲级 1086 Tree Traversals Again (25分)(先序中序链表建树,求后序)***重点复习
1086 Tree Traversals Again (25分) An inorder binary tree traversal can be implemented in a non-recu ...
- PAT 甲级 1086 Tree Traversals Again
https://pintia.cn/problem-sets/994805342720868352/problems/994805380754817024 An inorder binary tree ...
- 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 ...
- 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 ...
- 数据结构课后练习题(练习三)7-5 Tree Traversals Again (25 分)
7-5 Tree Traversals Again (25 分) An inorder binary tree traversal can be implemented in a non-recu ...
- PAT 甲级 1020 Tree Traversals (25分)(后序中序链表建树,求层序)***重点复习
1020 Tree Traversals (25分) Suppose that all the keys in a binary tree are distinct positive intege ...
- PAT 甲级 1020 Tree Traversals (25 分)(二叉树已知后序和中序建树求层序)
1020 Tree Traversals (25 分) Suppose that all the keys in a binary tree are distinct positive integ ...
- PAT 甲级 1020 Tree Traversals (二叉树遍历)
1020. Tree Traversals (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Suppo ...
- PAT甲级——1130 Infix Expression (25 分)
1130 Infix Expression (25 分)(找规律.中序遍历) 我是先在CSDN上面发表的这篇文章https://blog.csdn.net/weixin_44385565/articl ...
随机推荐
- python3练习100题——026
原题链接:http://www.runoob.com/python/python-exercise-example26.html 题目:利用递归方法求5!. 是25题递归方式的简化版所以很容易. 我的 ...
- android 获取webview内容真实高度(webview上下可滚动距离)
正常获取: mainWebView.getContentHeight()//获取html高度 mainWebView.getScale()//手机上网页缩放比例 mainWebView.getHeig ...
- 【算法学习记录-排序题】【PAT A1062】Talent and Virtue
About 900 years ago, a Chinese philosopher Sima Guang wrote a history book in which he talked about ...
- DataFrames,Datasets,与 SparkSQL
v\:* {behavior:url(#default#VML);} o\:* {behavior:url(#default#VML);} w\:* {behavior:url(#default#VM ...
- Light Up Your Business Promotions With LED Keychain
Imagine you want to insert the car key into the keyhole in the dark. What would you do? You will def ...
- 网络、芯片、专利、产业链……影响5G手机走势的因素有哪些?
近段时间,备受关注的5G手机迎来一个爆发的小高潮.中国质量认证中心官网显示8款5G手机获得3C认证.其中华为有4款 ,一加.中兴.OPPO和vivo各有一款5G手机获得3C认证.随后在7月23日,中兴 ...
- window10配置远程虚拟机window7上的mysql5.7数据源
原文链接:http://www.xitongcheng.com/jiaocheng/win10_article_18644.html windows10系统用户想要在电脑中设置ODBC数据源,于是手动 ...
- python 字符串的一些函数
split()函数 split() 以 空格 为分割符分割字符串,返回列表 split('_') 以'_'为分割符分割字符串,返回列表 strip() 函数 去掉前后的空格 下面是字符 ...
- execute command denied to user 'maintain'@'%' for routine
GRANT ALL PRIVILEGES ON *.* TO 'maintain'@'%' ; FLUSH PRIVILEGES;
- c#中的栈(stack)与队列(queue)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...