zoukankan      html  css  js  c++  java
  • PAT甲级——A1020 Tree Traversals

    Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and inorder traversal sequences, you are supposed to output the level order traversal sequence of the corresponding binary tree.

    Input Specification:

    Each input file contains one test case. For each case, the first line gives a positive integer N (≤), the total number of nodes in the binary tree. The second line gives the postorder sequence and the third line gives the inorder sequence. All the numbers in a line are separated by a space.

    Output Specification:

    For each test case, print in one line the level order traversal sequence of the corresponding binary tree. 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:

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

    Sample Output:

    4 1 6 3 5 7 2


    已知后序遍历和中序遍历输出层序遍历

     1 #include <iostream>
     2 #include <queue>
     3 using namespace std;
     4 int *pos, *ord;//存放后序和中序遍历数据
     5 struct Node
     6 {
     7     int val;
     8     Node *l, *r;
     9     Node(int a = 0) :val(a), l(nullptr), r(nullptr) {}
    10 };
    11 Node*  createTree(int posL,int posR, int ordL, int ordR)
    12 {
    13     if (posL > posR)
    14         return nullptr;
    15     Node *root = new Node();
    16     root->val = pos[posR];//根节点值
    17     int k;
    18     for (k = ordL; k <= ordR; ++k)
    19     {
    20         if (ord[k] == pos[posR])//找到原树的根
    21             break;
    22     }
    23     int numL = k - ordL;//左子树节点数量
    24     //递归构造左子树
    25     root->l = createTree(posL, posL + numL - 1, ordL, k - 1);
    26     //递归构造右子树
    27     root->r = createTree(posL + numL, posR - 1, k + 1, ordR);//取出根节点
    28     return root;
    29 }
    30 void getResBFS(Node* root)
    31 {
    32     queue<Node*>q;
    33     Node* p = nullptr;
    34     q.push(root);
    35     cout << root->val;
    36     while (!q.empty())
    37     {
    38         p = q.front();
    39         if (p != root)
    40             cout << " " << p->val;
    41         q.pop();
    42         if (p->l != nullptr)
    43             q.push(p->l);
    44         if (p->r != nullptr)
    45             q.push(p->r);
    46     }
    47     cout << endl;
    48 }
    49 
    50 int main()
    51 {
    52     int N;
    53     cin >> N;
    54     pos = new int[N];
    55     ord = new int[N];
    56     for (int i = 0; i < N; ++i)
    57         cin >> pos[i];
    58     for (int i = 0; i < N; ++i)
    59         cin >> ord[i];
    60     Node* root = createTree(0, N - 1, 0, N - 1);
    61     getResBFS(root);
    62     return 0;    
    63 }
  • 相关阅读:
    浅谈 facebook .net sdk 应用
    数据库中各种字符串的截取函数
    初、中、高级测试工程师面试题汇总(附答案)
    包管理器 scoop
    ModuleNotFoundError: No module named 'pymouse'解决办法
    Python 正则表达式(分组)
    7个Python实战项目代码,让你分分钟晋级大神!
    经典SQL查询题目
    linux中软件的安装和卸载方法
    测试常用工具
  • 原文地址:https://www.cnblogs.com/zzw1024/p/11228888.html
Copyright © 2011-2022 走看看