PAT [A1020]-Tree Traversals
来源:互联网 发布:mac后台程序关不上 编辑:程序博客网 时间:2024/06/05 14:55
PAT [A1020]-Tree Traversals
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 1:
7
2 3 1 5 7 6 4
1 2 3 4 5 6 7
Sample Output 1:
4 1 6 3 5 7 2
解题思路:
递归构造二叉树(后序中序确定一颗二叉树),并利用BFS输出层次遍历。
AC代码:
#include <cstdio>#include <queue>using namespace std;const int maxn = 50;int N, postOrder[maxn], inOrder[maxn];struct node{ int v; node* lChild; node* rChild;};node* create(int lp, int rp, int li, int ri){ if (lp > rp) return NULL; node* root = new node; root->v = postOrder[rp]; int index; for (int i = li; i <= ri; i++){ if (inOrder[i] == postOrder[rp]){ index = i; break; } } root->lChild = create(lp, index - 1 - li + lp, li, index - 1); root->rChild = create(rp-ri+index, rp-1, index + 1, ri); return root;}void BFS(node* root){ int cnt = 0, ans[maxn]; queue<node*> que; que.push(root); while (!que.empty()){ node *t = que.front(); ans[cnt++] = t->v; que.pop(); if(t->lChild != NULL) que.push(t->lChild); if (t->rChild != NULL) que.push(t->rChild); } for (int i = 0; i < cnt; i++){ printf("%d", ans[i]); if (i < cnt - 1) printf(" "); }}int main(){ freopen("C:\\Users\\Administrator\\Desktop\\test.txt", "r", stdin); while (scanf("%d", &N) != EOF){ for (int i = 0; i < N; i++){ scanf("%d", &postOrder[i]); } for (int i = 0; i < N; i++){ scanf("%d", &inOrder[i]); } node* root = create(0, N-1, 0, N-1); BFS(root); printf("\n"); } fclose(stdin); return 0;}
- PAT A1020. Tree Traversals
- PAT [A1020]-Tree Traversals
- 【PAT】A1020. Tree Traversals (25)
- PAT A1020. Tree Traversals (25)
- PAT甲级练习题A1020. Tree Traversals (25)
- PAT A1020 tree traversals (25)
- A1020. Tree Traversals (25)
- PAT A1020 Tree Traversals 根据先序和中序求层序
- pat 1020 Tree Traversals
- PAT 1020 Tree Traversals
- 【PAT】1020. Tree Traversals
- PAT 1020. Tree Traversals
- PAT 1020. Tree Traversals
- PAT 1020 Tree Traversals
- PAT--1020. Tree Traversals
- PAT 1020. Tree Traversals
- PAT 1020. Tree Traversals
- 【PAT】1020. Tree Traversals
- socket、websocket后台信息日志输出使用
- 2017 ACM-ICPC 亚洲区(西安赛区)网络赛 C.Sum
- Java反射机制
- Android Studio 手动下载 gradle 版本地址
- Markdown 高级语法进阶-zybuluo
- PAT [A1020]-Tree Traversals
- 引导页
- java_Throwable继承树
- spring-cloud-build-项目pom结构1.3.4(Dalston.SR3)-boot(1.5.6) 学习笔记
- CSS之文字溢出隐藏
- PAT 1127. ZigZagging on a Tree (30) 树的构建+特殊遍历序列
- 【找工作】笔试面试题总结——多益网络面试题(人工智能岗)
- 用户从控制台输入一行字符串,程序输出最长的连续字母串的长度和并把它输出。
- freebsd更新+vmware