zoukankan      html  css  js  c++  java
  • PAT 1020 Tree Traversals (25)

    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 (<=30), 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

    题目大意:通过后序遍历和中序遍历,输出层序遍历
    注意点:层序遍历的时候,添加到队列中的是结点序号,而不是结点的值, 压入层序遍历结果的才是结点的值
    思路:通过后序遍历和中序遍历反推出二叉树的结构, 把二叉树的结构保存在邻接表中, 用bfs就能和方便的得到层序遍历
      要构一棵树,就是要先找根节点, 再找其左右节点; 通过后序序列可以很容易的找到根节点, 即后序序列的最后一个节点;
      那么怎么找左右节点呢? 根节点的左节点是其左子树的根节点, 即左子树后序遍历的最后一个节点, 右节点也是如此; 可以发现, 构建树是一个重复递归的过程, 即找子树的根节点, 亦即找左子树后序遍历的最后一个节点
    那又怎么确定左子树, 右子树区间呢? 通过后序遍历的规律可以知道, 左子树遍历的结果在数组中一定是连续的, 只要知道左子树, 右子树的节点个数就能找到其区间
      那么怎么知道左子树, 右子树节点个数呢? 这时就要用到中序遍历了, 中序遍历先遍历左子树, 再遍历根节点, 再遍历右子树; 那么只要知道根节点, 找到根节点再中序遍历的位置,记为i, 那么i左边的就是左子树, i右边的就是右子树节点
    通过inl, i, inr这三者的关系就能求出左右子树的节点个数; 从而可以进一步确定左子树右子树再后序遍历中的区间位置;
    所以构建树的过程可以分为两步:
      1.找子树的根节点
      2.找左右子树的节点个数
    重复的进行1,2两个步骤就能构建出一颗完整的树



     1 #include<iostream>
     2 #include<vector>
     3 #include<queue>
     4 using namespace std;
     5 //post, in, level分别记录后序遍历, 中序遍历, 层序遍历的结果
     6 vector<int> post(30), in(30), level;
     7 //tree: 用邻接表来记录树的结构, 第一个下标表示父节点的序号, tree[i][0]
     8 //tree[i][1]分别表示左节点和右节点的序号, 当值为-1的时候,表示该节点为空
     9 vector<vector<int> > tree(30, vector<int>(2, -1));
    10 int root;  //根节点, 便于求层序遍历
    11 
    12 void dfs(int &index, int inl, int inr, int postl, int postr){
    13   if(inl>inr) return ;
    14   index = postr;
    15   int temp=post[postr], i=inl;
    16   while(i<=inr && in[i]!=temp) i++;
    17   dfs(tree[index][0], inl, i-1, postl, postl+(i-inl)-1);
    18   dfs(tree[index][1], i+1, inr, postl+i-inl, postr-1);
    19 }
    20 
    21 void getLevelOrder(){
    22   queue<int> q;
    23   q.push(root);
    24   while(q.size()){
    25     int temp=q.front();
    26     q.pop();
    27     level.push_back(post[temp]);
    28     if(tree[temp][0]!=-1) q.push(tree[temp][0]);
    29     if(tree[temp][1]!=-1) q.push(tree[temp][1]);
    30   }
    31 }
    32 int main(){
    33   int n, i;
    34   scanf("%d", &n);
    35   for(i=0; i<n; i++) cin>>post[i];
    36   for(i=0; i<n; i++) cin>>in[i];
    37   dfs(root, 0, n-1, 0, n-1);
    38   getLevelOrder();
    39   printf("%d", level[0]);
    40   for(i=1; i<n; i++) printf(" %d", level[i]);
    41   return 0;
    42 }
    有疑惑或者更好的解决方法的朋友,可以联系我,大家一起探讨。qq:1546431565
  • 相关阅读:
    Java数据类型
    redis的安装
    软件测试(一、二)
    软件开发
    python----基础函数
    Python的web框架
    Python 中的lambda函数介绍
    Python中HTTP协议
    Django基本模块介绍
    Python --------列表
  • 原文地址:https://www.cnblogs.com/mr-stn/p/9214298.html
Copyright © 2011-2022 走看看