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:

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

Sample Output 1:

Yes
2 1 6 4 7 3 5

Sample Input 2:

4
1 2 3 4
2 4 3 1

Sample Output 2:

No
2 1 3 4
 #include <iostream>
#include <vector>
using namespace std;
int n, a;
vector<int>preOrder, postOrder, inOrder;
bool flag = true;//表示树的形态不唯一
void getInOrder(int root, int left, int right)
{
if (left >= right)
{
if (left == right)//只有一个节点
inOrder.push_back(preOrder[root]);
return;
}
int i = left;
while (i < right && preOrder[root + ] != postOrder[i])//查找前序遍历中下一个节点在后序中的位置
++i;
if (i == right - )//先根序列中根节点的下一结点在后根序列中的位置正好等于right-1
flag = false;
getInOrder(root + , left, i);
inOrder.push_back(preOrder[root]);
getInOrder(root + i - left + , i + , right - );
}
int main()
{
cin >> n;
for (int i = ; i < n; ++i)
{
cin >> a;
preOrder.push_back(a);
}
for (int i = ; i < n; ++i)
{
cin >> a;
postOrder.push_back(a);
}
getInOrder(, , n - );
cout << (flag ? "Yes" : "No") << endl;
for (int i = ; i < n; ++i)
cout << (i > ? " " : "") << inOrder[i];
cout << endl;
return ;
}

最新文章

  1. T1加权像(T1 weighted image,T1WI)
  2. 根据Unicode编码用C#语言把它转换成汉字的代码
  3. 4.4 spring-自定义标签的解析
  4. h5拖放-ff的bug
  5. centos账户管理命令(root权限)
  6. qt模型学习
  7. 《转》如何成为一个牛逼的C/C++程序员?
  8. 【canvas学习笔记一】基本认识
  9. Spring设计模式_工厂模式
  10. subing用法
  11. Java垃圾回收机制和注解
  12. 利用R与SAS进行关联规则挖掘
  13. C++自动类型转化--特殊构造函数方法和重载的运算符方法
  14. php解析Excel表格并且导入MySQL数据库
  15. leetcode560
  16. Mac配置Eclipse CDT的Debug出现的问题(转)
  17. iOS 在viewDidLayoutSubviews自动布局crash问题
  18. QT5 样式随笔
  19. [转载]在VirtualBox中收缩虚拟磁盘映像文件
  20. Activity的onPause()、onStop()和onDestroy()里要做的事情

热门文章

  1. HIVE常用SQL语句及语法
  2. NX二次开发-UFUN拉伸函数UF_MODL_create_extruded2
  3. 执行SQL语句---SELECT
  4. BZOJ3211花神游历各国-线段树&amp;树状数组-(HDU4027同类型)
  5. 笔试题-求小于等于N的数中有多少组素勾股数
  6. Web 开发规范 — WSGI
  7. git相关操作。
  8. ECMAScript1.3 数组 | 函数 | 作用域 | 预解析
  9. MapReduce1.0的缺陷
  10. log4j.rootLogger