PAT 1119 Pre- and Post-order Traversals (30分)

1119 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

 先序和后序得出的中序 不唯一

如果唯一 输出 yes 如果不唯一 输出no 

输出中序序列,多个输出任意一个

//先序  后序  --> 中序
//如果是 唯一的输出 Yes 不是输出No
//输出中序序列 
#include 
#define Max 33
using namespace std;
int pre[Max], post[Max], n;
struct node{
	int data;
	struct node *lchild, *rchild;
}; 
vector ans;
int flag = 1;

node *create(int prel, int prer, int postl, int postr){
	if(prel > prer) return NULL;
	//先序的开始的第一个应该是后序的最后一个是相等的,这个结点就是根结点
	node *rt = new node();
	rt->data = pre[prel]; 
	rt->lchild = NULL;
	rt->rchild = NULL;
	if(prel == prer) return rt;
	//找左右子树的范围
	//在先序里面找到右子树根节点的位置
	int i;
	//以后序的根结点的前面一个结点作为参考,寻找这个结点在先序的位置,就可以根据这个位置来划分左右孩子
	for(i = prel; i <= prer; i++){
		if(post[postr - 1] == pre[i])
			break;
	} 
	if(i - prel > 1){
		//左子树:prel+1, i - 1   i-1-(prel + 1)+1 --> i-1-prel  
		rt->lchild = create(prel+1, i-1, postl, postl+i-prel-2); 
		//右子树 
		rt->rchild = create(i, prer, postl+i-prel-1, postr-1);
	}else { // i == prel + 1 左右子树的根节点重合了 
		flag = 0;
		rt->rchild = create(i, prer, postl+i-prel-1, postr-1); 
	}
	return rt;
}
void inorder(node *rt){
	if(!rt) return;
	inorder(rt->lchild);
	ans.push_back(rt->data);
	inorder(rt->rchild);
}
int main(){
	ans.clear();
	cin >> n;
	for(int i = 0; i < n; i++) cin >> pre[i];
	for(int j = 0; j < n; j++) cin >> post[j];
	node *rt = create(0, n-1, 0, n-1);
	inorder(rt);
	if(flag) cout << "Yes" << endl;
	else cout << "No" << endl;
	for(int i = 0; i < ans.size(); i++){
		cout << ans[i];
		if(i != ans.size() - 1) cout << " "; 
	}
	cout << endl;
	return 0;
} 

 

你可能感兴趣的:(PAT,数据结构)