1086 Tree Traversals Again (25 分)(二叉树的遍历)
用栈来模拟一棵二叉树的先序遍历和中序遍历过程,求这棵二叉树的后序遍历
由题棵知道:push是先序遍历
pop是中序遍历
#include<bits/stdc++.h> using namespace std;
vector<int>pre;
vector<int>in;
vector<int>vec;
const int N=;
int pre1[N];
int in1[N];
void print(int l1,int r1,int l2,int r2)
{
if(l1>r1||l2>r2) return;
int mid=l2;
while(in[mid]!=pre[l1]) mid++;
print(l1+,l1+mid-l2,l2,mid-);
print(l1+mid-l2+,r1,mid+,r2);
vec.push_back(pre[l1]);
}
int main()
{
int n;
scanf("%d",&n);
stack<int>st;
for(int i=;i<*n;i++){
char s[];
scanf("%s",s);
if(strcmp(s,"Push")==){
int num;
scanf("%d",&num);
st.push(num);
pre.push_back(num);
}
else{
int t=st.top();
st.pop();
in.push_back(t);
}
} print(,n-,,n-);
for(int i=;i<vec.size();i++){
if(i) printf(" ");
printf("%d",vec[i]);
}
printf("\n");
return ;
}
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 (25 分)(树知二求一)
题意:输入一个正整数N(<=30),接着输入2*N行表示栈的出入(入栈顺序表示了二叉搜索树的先序序列,出栈顺序表示了二叉搜索树的中序序列),输出后序序列. AAAAAccepted code: ...
- 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 ...
- 03-树3. Tree Traversals Again (25)将先序遍历和中序遍历转为后序遍历
03-树3. Tree Traversals Again (25) 题目来源:http://www.patest.cn/contests/mooc-ds/03-%E6%A0%913 An inorde ...
- PAT A1020 Tree Traversals (25 分)——建树,层序遍历
Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and i ...
- 1020 Tree Traversals (25 分)(二叉树的遍历)
给出一个棵二叉树的后序遍历和中序遍历,求二叉树的层序遍历 #include<bits/stdc++.h> using namespace std; ; int in[N]; int pos ...
- A1020 Tree Traversals (25 分)
Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and i ...
- 1086. Tree Traversals Again (25)
题目如下: An inorder binary tree traversal can be implemented in a non-recursive way with a stack. For e ...
- 1086. Tree Traversals Again (25)-树的遍历
题意:用栈的push.pop操作给出一棵二叉树的中序遍历顺序,求这棵二叉树的后序遍历. 需要一个堆结构s,一个child变量(表示该节点是其父亲节点的左孩子还是右孩子),父亲节点fa对于push v操 ...
随机推荐
- System.Web.Caching.Cache
此类是利用缓存来保存信息的.可以把一些稳定的数据,不会随用户而改变的信息利用Cache保存起来,可以优化网站的速度. Cache辅助类已上传:GitHub Cache和Session,cookie的区 ...
- python main
python中的main函数,总体来说就是,main比较适合写test测试,有点类似于java中的testcase,就是程序单独运行时是运行main的,但是当被调用时就不会运行main了.具体可以参考 ...
- c/c++基础
如果有你认为重要的知识点,而我这却没有记录下来的,那么期待你分享给我(^U^)ノ~YO. 1.在结构体中,符号->的前面是指针变量,符号.的前面是普通变量. 程序中a->b等价于(*a ...
- 1.Netty入门
Netty入门 1.Netty介绍 (1)百度百科介绍: Netty是由JBOSS提供的一个java开源框架.Netty提供异步的.事件驱动的网络应用程序框架和工具,用以快速开发高性能.高可靠性的网络 ...
- 在Linux文件清空的几种方法
在Linux文件清空的几种方法 1.使用重定向的方法 [root@centos7 ~]# du -h test.txt 4.0K test.txt [root@centos7 ~]# > tes ...
- datatable设置动态宽度,超过一定长度出现滚动条
获得宽度:var tableAutoWidth = $('.dataTable_wrapper').width();if (tableAutoWidth < 1200) { tableAutoW ...
- NuGet管理和还原程序包
在很多开源的程序下载下来不能使用,一般都是平台X86 和X64没有修改,还一个就是程序缺少资源包文件.用Nuget还原即可: 一般建议先修改好平台,然后用NuGet还原程序包.
- LeetCode961 重复 N 次的元素
问题: 重复 N 次的元素 在大小为 2N 的数组 A 中有 N+1 个不同的元素,其中有一个元素重复了 N 次. 返回重复了 N 次的那个元素. 示例 1: 输入:[1,2,3,3] 输出:3 示例 ...
- IDEA 编辑框光标闪烁
依次打开如下菜单: File -> Settings -> Editor -> General -> Appearance -> 选中 Caret blinking (m ...
- 关于IT术语---ip、uv、pv、tps、qps、rps
涉及到IT方面的几条术语,这里要好好说道说道: 只要和网站打交道,难免会经常听到一系列的转有名词 >>> 系统今日UV多少.PV多少.QPS多少之类的问题.这里就对这些常见的术语 ...