【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 ...
随机推荐
- javascript 循环读取数组中的值
//数组 var a = ["#F85C6F", "#78B0F0", "#DB83ED", "#8EC656", &q ...
- goto语句的本质
除非跳出多个循环嵌套和远程注入技术,否则尽量少用goto goto会降低程序的可读性,让代码难以调试 利用递归也可以实现循环结构和do while类似 #define _CRT_SECURE_NO_W ...
- 线性混合+ROI
相关代码: #include <opencv2/opencv.hpp> #include <iostream> using namespace cv; using namesp ...
- ASP.NET + MVC5 入门完整教程七 -—-- MVC基本工具(上)
https://blog.csdn.net/qq_21419015/article/details/80474956 这里主要介绍三类工具之一的 依赖项注入(DI)容器,其他两类 单元测试框架和模仿工 ...
- ignoreContentAdaptWithSize
使用cocosStudio 中 的Text控件时,要设定text.ignoreContentAdaptWithSize(true); 设定控件(0.5,0.5)锚点位置不随文本长度变化而变化.
- 常见的sql语句练习
一. 1.新建表 test id varchar2(20)name varchar2(20)addr varchar2(50)score number create table test(id var ...
- python下matplotlib、numpy、pandas联合作图逐步深入分析
1.代码1: from pandas import Series,DataFrame from numpy.random import randn import numpy as np import ...
- ACM的探索之Everything is Generated In Equal Probability! 后序补充丫!
Problem Desciption: 百度翻译后的汉化: 参见博客:https://www.cnblogs.com/zxcoder/p/11253099.html https://blog.csdn ...
- MFC程序使用控制台打印
1.在OnCreate窗口创建方法中调用控制台窗口创建方法,创建的窗口是与MFC主窗口共存亡的 参考地址:https://blog.csdn.net/Yong_Qi2015/article/detai ...
- 上传图片时使用crop进行裁剪
上传图片时,往往需要对图片进行裁剪. 实现方法为: 1.引入crop.css body{background:#}.upload-container{position:absolute;left:%; ...