PAT (Advanced Level) Practise 1020. Tree Traversals (25)
来源:互联网 发布:2015最新网络神曲 编辑:程序博客网 时间:2024/04/29 19:23
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 (<=30), 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:72 3 1 5 7 6 41 2 3 4 5 6 7Sample Output:
4 1 6 3 5 7 2
提交代码
#include <iostream>using namespace std;int main(){ int n,i,t,w,flag=1; int postorder[40]; int inorder[40]; int a[40],b[40],c[40],d[40]; cin>>n; for(i=1;i<=n;i++) cin>>postorder[i]; for(i=1;i<=n;i++) cin>>inorder[i]; t=0; w=0; a[t]=1; b[t]=n; c[t]=1; d[t]=n; while(t<=w) { if(flag) { cout<<postorder[b[t]]; flag=0; } else cout<<" "<<postorder[b[t]]; for(i=c[t];i<=d[t];i++) if(postorder[b[t]]==inorder[i]) { if(i-1>=c[t]) { w++; a[w]=a[t]; b[w]=a[t]+i-c[t]-1; c[w]=c[t]; d[w]=i-1; } if(i+1<=d[t]) { w++; a[w]=a[t]+i-c[t]; b[w]=b[t]-1; c[w]=i+1; d[w]=d[t]; } break; } t++; } system("pause"); return 0;}
- 1020. Tree Traversals (25) @ PAT (Advanced Level) Practise
- PAT (Advanced Level) Practise 1020. Tree Traversals (25)
- 1020. Tree Traversals (25)——PAT (Advanced Level) Practise
- PAT (Advanced Level) Practise 1020. Tree Traversals (25)
- 浙大PAT (Advanced Level) Practise 1020Tree Traversals (25)
- PAT (Advanced Level) Practise 1020 Tree Traversals (25)
- PAT (Advanced Level) Practise 1086 Tree Traversals Again (25)
- PAT (Advanced Level) Practise 1086 Tree Traversals Again (25)
- PAT (Advanced Level) Practise 1020 Tree Traversals (25)
- 【PAT Advanced Level】1020. Tree Traversals (25)
- PAT (Advanced Level) 1020. Tree Traversals (25)
- 【PAT】【Advanced Level】1020. Tree Traversals (25)
- 1086. Tree Traversals Again (25)【二叉树】——PAT (Advanced Level) Practise
- PAT Advanced Level 1020. Tree Traversals (25)(Java 实现)
- 浙大 PAT Advanced level 1020. Tree Traversals (25)
- 【PAT】【Advanced Level】1086. Tree Traversals Again (25)
- Pat(Advanced Level)Practice--1020(Tree Traversals)
- PAT (Advanced) 1020. Tree Traversals (25)
- java基础学习笔记(7)
- uva 140 Bandwidth 带宽
- linux下串口调试工具/串口终端推荐: picocom
- 2014-10-25Android学习--------Android应用工程文件组成
- Linux 增加sys节点技巧
- PAT (Advanced Level) Practise 1020. Tree Traversals (25)
- 判断数字是多少位数,正向反向输出
- 浅谈CSRF攻击方式
- 字符串循环移位
- 吴桐的新博客
- 这Hadoop分布式文件系统
- GET vs. POST
- libzplay简单的播放音频例子
- 2014——我的求职之路(四)