zoukankan      html  css  js  c++  java
  • PAT甲级——A1119 Pre- and Post-order Traversals【30】

    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


     1 #include <iostream>
     2 #include <vector>
     3 using namespace std;
     4 int n, a;
     5 vector<int>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-1
    19         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 }
  • 相关阅读:
    asp.net webapi 无法使用多个post,浏览器不支持put delete,405错误
    vs2019 nable-migrations : 无法将“enable-migrations”项识别为 cmdlet 使用“1”个参数调用“LoadFrom”时发生异常:“ EntityFramew
    javascript 扩展运算符(spread)三个点(...)的作用及用法
    PIE SDK元素的选择和取消选择
    PIE SDK元素的删除
    PIE SDK元素事件的监听
    PIE SDK临时元素的绘制
    PIE SDK图片元素的绘制
    PIE SDK文本元素的绘制
    PIE SDK面元素的绘制
  • 原文地址:https://www.cnblogs.com/zzw1024/p/11462895.html
Copyright © 2011-2022 走看看