博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
PAT甲级——A1119 Pre- and Post-order Traversals【30】
阅读量:4541 次
发布时间:2019-06-08

本文共 2650 字,大约阅读时间需要 8 分钟。

Suppose that all the keys in a binary tree are distinct positive integers. A unique binary tree can be determined by a given pair of postorder and inorder traversal sequences, or preorder and inorder traversal sequences. However, if only the postorder and preorder traversal sequences are given, the corresponding tree may no longer be unique.

Now given a pair of postorder and preorder traversal sequences, you are supposed to output the corresponding inorder traversal sequence of the tree. If the tree is not unique, simply output any one of them.

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 preorder sequence and the third line gives the postorder sequence. All the numbers in a line are separated by a space.

Output Specification:

For each test case, first printf in a line Yes if the tree is unique, or No if not. Then print in the next line the inorder traversal sequence of the corresponding binary tree. If the solution is not unique, any answer would do. It is guaranteed that at least one solution exists. 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:

71 2 3 4 6 7 52 6 7 4 5 3 1

Sample Output 1:

Yes2 1 6 4 7 3 5

Sample Input 2:

41 2 3 42 4 3 1

Sample Output 2:

No2 1 3 4
1 #include 
2 #include
3 using namespace std; 4 int n, a; 5 vector
preOrder, postOrder, inOrder; 6 bool flag = true;//表示树的形态不唯一 7 void getInOrder(int root, int left, int right) 8 { 9 if (left >= right)10 {11 if (left == right)//只有一个节点12 inOrder.push_back(preOrder[root]);13 return;14 }15 int i = left;16 while (i < right && preOrder[root + 1] != postOrder[i])//查找前序遍历中下一个节点在后序中的位置17 ++i;18 if (i == right - 1)//先根序列中根节点的下一结点在后根序列中的位置正好等于right-119 flag = false;20 getInOrder(root + 1, left, i);21 inOrder.push_back(preOrder[root]);22 getInOrder(root + i - left + 2, i + 1, right - 1);23 }24 int main()25 {26 cin >> n;27 for (int i = 0; i < n; ++i)28 {29 cin >> a;30 preOrder.push_back(a);31 }32 for (int i = 0; i < n; ++i)33 {34 cin >> a;35 postOrder.push_back(a);36 }37 getInOrder(0, 0, n - 1);38 cout << (flag ? "Yes" : "No") << endl;39 for (int i = 0; i < n; ++i)40 cout << (i > 0 ? " " : "") << inOrder[i];41 cout << endl;42 return 0;43 }

 

转载于:https://www.cnblogs.com/zzw1024/p/11462895.html

你可能感兴趣的文章
hdu 1704 Rank(floyd传递闭包)
查看>>
Educational Codeforces Round 27 G. Shortest Path Problem?(Guass异或线性基)
查看>>
【BZOJ3622】已经没有什么好害怕的了(动态规划+广义容斥)
查看>>
HDOJ 1023 Train Problem II
查看>>
途牛订单的服务化演进
查看>>
软件工程之四则运算
查看>>
ABAP 根据权限显示或隐藏状态栏的按钮
查看>>
跑步计划
查看>>
mvc中使用uploadify批量上传的应用
查看>>
Kibana查询说明
查看>>
[AHOI 2009]chess 中国象棋
查看>>
UVA 11990 ”Dynamic“ Inversion(线段树+树状数组)
查看>>
Hibernate学习四----------Blob
查看>>
CTF-练习平台-Misc之 中国菜刀,不再web里?
查看>>
Mac系统配置JDK环境变量
查看>>
多项式累加
查看>>
剑指offer(18)二叉搜索树的后续遍历
查看>>
微信小程序一笔记账开发进度四
查看>>
bzoj 1070 费用流
查看>>
201671010139 徐楠 第四周总结
查看>>