浙大PAT甲级-1020
来源:互联网 发布:java jdk1.7下载地址 编辑:程序博客网 时间:2024/05/29 03:51
二叉树的后缀,中缀和层次表示
已知后缀,中缀,求其层次
解决方法:递归
#include <iostream>#include <vector>using namespace std;int n;vector<int> level[31];int num[31]={0};int post[30]={0}, in[30]={0};void pre(int post1,int post2,int in1,int in2,int lev){ if(post1>post2||in1>in2||lev>n) return; level[lev].push_back(post[post2]); num[lev]++; int i; for(i=in1;i<=in2;i++){ if(in[i]==post[post2]) break; } pre(post1,post1+i-in1-1,in1,in1+i-in1-1,lev+1); pre(post1+i-in1,post2-1,i+1,in2,lev+1);}int main(){ int i; cin>>n; for(i=0;i<n;i++) cin>>post[i]; for(i=0;i<n;i++) cin>>in[i]; pre(0,n-1,0,n-1,1); cout<<level[1][0]; for(i=2;i<31;i++){ for(int j=0;j<num[i];j++){ if(level[i][j]==0) continue; cout<<" "<<level[i][j]; } } return 0;}
阅读全文
0 0
- 浙大PAT甲级-1020
- 浙大pat甲级 1023
- 浙大pat甲级 1024
- 浙大pat甲级 1025
- 浙大PAT甲级 1026
- 浙大PAT甲级 1028
- 浙大PAT甲级 1029
- 浙大PAT甲级 1030
- 浙大PAT甲级 1031
- 浙大PAT甲级 1032
- 浙大PAT甲级 1033
- 浙大PAT甲级 1035
- 浙大PAT甲级 1037
- 浙大PAT甲级 1039
- 浙大PAT甲级 1040
- 浙大PAT甲级 1038
- 浙大PAT甲级 1041
- 浙大PAT甲级 1042
- 关于阿里云服务器中Tomcat的<Host name="localhost">中的localhost改为外网ip无法访问的问题
- 设计模式
- 欢迎使用CSDN-markdown编辑器
- redis常用命令
- vue从入门到放弃--- 时间戳 跨域
- 浙大PAT甲级-1020
- 第五章 JavaScript HTML DOM
- 各个历史版本 cuda toolkit 下载链接
- 2017-07-05(servlet)
- Exception sending context initialized event to listener instance of class org.springframework.web.c
- <input type="image">标签引发的form表单自动提交
- Android adb安装apk时提示Invalid APK file
- Eclipse中导入其他来源的project时报错:setting build path has encountered a problem的解决方法
- 如何编译各平台使用的库:以编译tolua为例