• 1119 Pre- and Post-order Traversals


    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:

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

    Sample Output 1:

    1. Yes
    2. 2 1 6 4 7 3 5

    Sample Input 2:

    1. 4
    2. 1 2 3 4
    3. 2 4 3 1

    Sample Output 2:

    1. No
    2. 2 1 3 4
    1. #include
    2. using namespace std;
    3. int n, pre[50], post[50], in[50], cnt;
    4. bool flag;
    5. struct node {
    6. int left, right;
    7. } tree[50];
    8. int inorder(int prel, int prer, int postl, int postr) {
    9. if (prel == prer) {
    10. return pre[prel];
    11. }
    12. if (pre[prel + 1] == post[postr - 1]) { //不唯一的情况
    13. flag = 1;
    14. tree[pre[prel]].left = inorder(prel + 1, prer, postl, postr - 1);
    15. } else { //唯一的情况
    16. int t = postl;
    17. while (post[t] != pre[prel + 1]) { //寻找后序遍历中左子树的根
    18. t++;
    19. }
    20. tree[pre[prel]].left = inorder(prel + 1, prel + t - postl + 1, postl, t); //左子树
    21. tree[pre[prel]].right = inorder(prel + t - postl + 2, prer, t + 1, postr - 1); //右子树
    22. }
    23. return pre[prel];
    24. }
    25. void dfs(int x) {
    26. if (tree[x].left != 0) {
    27. dfs(tree[x].left);
    28. }
    29. in[cnt++] = x;
    30. if (tree[x].right != 0) {
    31. dfs(tree[x].right);
    32. }
    33. }
    34. int main() {
    35. cin >> n;
    36. for (int i = 0; i < n; i++) {
    37. cin >> pre[i];
    38. }
    39. for (int i = 0; i < n; i++) {
    40. cin >> post[i];
    41. }
    42. inorder(0, n - 1, 0, n - 1);
    43. dfs(pre[0]);
    44. if (flag) {
    45. cout << "No" << endl;
    46. } else {
    47. cout << "Yes" << endl;
    48. }
    49. for (int i = 0; i < cnt; i++) {
    50. cout << in[i];
    51. if (i != cnt - 1) {
    52. cout << ' ';
    53. }
    54. }
    55. cout << endl;
    56. return 0;
    57. }

     参考博客:【后序遍历】+【先序遍历】与【中序遍历】的关系 【PAT】1119 Pre- and Post-order Traversals_Offer.harvester的博客-CSDN博客_后序序列和中序序列的关系

  • 相关阅读:
    1.4_7 Axure RP 9 for mac 高保真原型图 - 案例6 【旋转的唱片4】进度条_拖拽、点击
    springboot实现全局事务管理
    【推荐系统】特征处理
    服务器性能高低判断
    Linux系统LVM操作
    用acme.sh给网站域名,申请免费SSL永久证书(自动续期)
    C++ 小游戏 视频及资料集(5)
    MarkDown基础及表格、KaTeX公式、矩阵、流程图、UML图、甘特图语法
    【英语阅读】
    【ES6】阮一峰ES6学习(五)Set和Map联系及区别
  • 原文地址:https://blog.csdn.net/weixin_53199925/article/details/127611133