• 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
    VoLTE端到端业务详解 | VoLTE网络
    spring5.0 源码解析(day07) registerListeners();
    【Axure高保真原型】附件卡片
    Three.js 实现简单的PCD加载器(可从本地读取pcd文件)【附完整代码】
    强化学习笔记
    AI篇-chatgpt基本用法(文心一言也适用)
    双指针代码
  • 原文地址:https://blog.csdn.net/weixin_53199925/article/details/127611133