1020 Tree Traversals (25 分)
 

Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and inorder traversal sequences, you are supposed to output the level order traversal sequence of the corresponding binary tree.

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N (≤), the total number of nodes in the binary tree. The second line gives the postorder sequence and the third line gives the inorder sequence. All the numbers in a line are separated by a space.

Output Specification:

For each test case, print in one line the level order traversal sequence of the corresponding binary tree. All the numbers in a line must be separated by exactly one space, and there must be no extra space at the end of the line.

Sample Input:

7
2 3 1 5 7 6 4
1 2 3 4 5 6 7

Sample Output:

4 1 6 3 5 7 2

这个推了蛮久的。。。。。不太熟练

    //左子树
if(k-l2==){
tree[root].l = -;//左子树为空
}
else{
buildTree(*root,l1,l1+k-l2-,l2,k-);
}
//右子树
if(k+>r2){
tree[root].r = -;//右子树为空
}
else{
buildTree(*root+,l1+k-l2,r1-,k+,r2);
}

AC代码:

#include<bits/stdc++.h>
using namespace std;
int post[];
int in[];
int n;
struct node{
int v;
int l;
int r;
}tree[];
queue<int>q;
void buildTree(int root,int l1,int r1,int l2,int r2){//后序,中序
//cout<<root<<" "<<l1<<"-"<<r1<<" "<<l2<<"-"<<r2<<endl;
//先找根节点
tree[root].v = post[r1];
if(l1==r1){ //只有一个节点
tree[root].l = -;
tree[root].r = -;
return;
}else{
tree[root].l = *root;
tree[root].r = *root+;
}
//找一下根在中序上的位置
int k;
for(int i=l2;i<=r2;i++){
if(in[i]==post[r1]){
k=i;//前面k-l2个数就是左子树
break;
}
}
//左子树
if(k-l2==){
tree[root].l = -;//左子树为空
}
else{
buildTree(*root,l1,l1+k-l2-,l2,k-);
}
//右子树
if(k+>r2){
tree[root].r = -;//右子树为空
}
else{
buildTree(*root+,l1+k-l2,r1-,k+,r2);
}
}
int main(){
int n;
cin>>n;
for(int i=;i<=n;i++){
cin>>post[i];
}
for(int i=;i<=n;i++){
cin>>in[i];
}
buildTree(,,n,,n);
//bfs 层序
while(!q.empty()) q.pop();
q.push();
while(!q.empty()){
node x=tree[q.front()];
q.pop();
cout<<x.v;
if(x.l!=-){
q.push(x.l);
}
if(x.r!=-){
q.push(x.r);
}
if(!q.empty()){
cout<<" ";
}
}
return ;
}

最新文章

  1. NET Core-学习笔记(一)
  2. Myeclipse不显示js文件错误的方法
  3. openerp7 时区问题
  4. xtrabackup备份rds记录
  5. Eclipse+Tomcat搭建https环境
  6. UVA 11419SAM I AM(输出 最小覆盖点 )
  7. 架构师速成-如何高效编程 for java
  8. http://docwiki.embarcadero.com/RADStudio/XE7/en/Delphi_Data_Types
  9. Systemd 入门教程:实战篇
  10. UVA 11549 Calculator Conundrum (Floyd判圈算法)
  11. HTML - HTML Commonly Used Character Entities
  12. hdu 折线切割平面 (java)
  13. github和本地仓库关联
  14. 在Ubuntu下运行 apt-get update命令后出现错误:
  15. Java垃圾回收(整理)
  16. P1439 最长公共子序列(nlognLCS问题)
  17. shiro教程2(自定义Realm)
  18. 方法名太多,使用方法的重载(overload)来解决
  19. 013 Spark中的资源调优
  20. iOS11适配

热门文章

  1. 快看,那个学SLAM 的崩溃了!
  2. Hadoop添加LZO压缩支持
  3. MySQL 进阶6: 连接查询 (多表连接) : 等值连接/非等值连接 /左右全连接/内连接
  4. 如何让DEV跳出的“提示试用版”的对话框不再显示
  5. Hibernate初探之单表映射——使用Junit进行测试
  6. linux如何查看所有的用户和组信息
  7. 原生JS实现简单富文本编辑器2
  8. Jquery 实现table标题点击复制整列td内容到剪贴板
  9. PHP mysqli_errno() 函数
  10. learning express step(十一)