PAT-A 真题 – 1119 Pre- and Post-order Traversals

发布于 / PAT-甲级 / 0 条评论

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

题目大意:给定二叉树的前序、后序遍历序列,求出任意中序遍历序列。如果唯一,输出Yes,否则输出No。

这道题有一些难度,关键在于什么时候中序遍历不唯一。

先抛开这个问题,利用前序遍历序列为“根左右”、后序遍历为“左右根”的形式,暴力写出递归构造二叉树的代码,会发现,对于题目中给的sample Input 2的输入,总会发生段错误,这时因为有的子树只有一个孩子,缺少了左子树或右子树,所以强行构造的话会发生越界。那么这个问题也就解决了,对于缺少一个子树的节点,我们无法判断缺少的是左子树还是右子树,也就不唯一了。

那么如何判断缺少子树呢?请看程序:

#include <iostream>
#include <algorithm>
#include <vector>
#include <cstdio>
using namespace std;
vector<int> pre, in, post;
bool flag = true;

int findIndex(vector<int> &v, int n){
  return find(v.begin(), v.end(), n) - v.begin(); 
}

void BuildTree(int preL, int preR, int postL, int postR){
  if(preL == preR){  //根节点的递归边界 
    in.push_back(pre[preL]);  //根节点放入容器
    return; 
  }
  if(preL > preR){  //缺少左子树或右子树的递归边界 
    flag = false;  //这时无法判断剩下的那个是左子树还是右子树,所以具有不确定性 
    return; 
  }
  //递归构造左子树
  int LeftChildPreL = preL + 1,
    LeftChildPreR = findIndex(pre, post[postR - 1]) - 1,
    LeftChildPostL= postL,
    LeftChildPostR= findIndex(post, pre[preL + 1]);
  BuildTree(LeftChildPreL, LeftChildPreR, LeftChildPostL, LeftChildPostR);
  //根节点
  in.push_back(pre[preL]);
  //递归构造右子树
  int RightChildPreL = LeftChildPreR + 1,
    RightChildPreR = preR,
    RightChildPostL= LeftChildPostR + 1,
    RightChildPostR= postR - 1;
  BuildTree(RightChildPreL, RightChildPreR, RightChildPostL, RightChildPostR);
}

int main(){
  int cnt;
  cin >> cnt;
  pre.resize(cnt), post.resize(cnt);
  for(int i = 0; i < cnt; i++)
    cin >> pre[i];
  for(int i = 0; i < cnt; i++)
    cin >> post[i];
  BuildTree(0, cnt-1, 0, cnt-1);
  printf("%s\n", flag ? "Yes" : "No");
  for(int i = 0; i < in.size(); i++){
    if(i != 0) cout << ' ';
    cout << in[i];
  }
  cout << endl;  //不加这个莫名其妙全是格式错误,原因不明 
  return 0;
}

转载原创文章请注明,转载自: 斐斐のBlog » PAT-A 真题 – 1119 Pre- and Post-order Traversals
目前还没有评论,快来抢沙发吧~