PAT-1119-树的转化

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:

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

谷歌中文翻译:

假设二叉树中的所有键都是不同的正整数。可以通过给定的一对后序和有序遍历序列或预序和有序遍历序列来确定唯一的二叉树。但是,如果仅给出后序遍历和前序遍历序列,则对应的树可能不再是唯一的。

现在给定一对后序遍历序列和前序遍历序列,您应该输出树的相应的有序遍历序列。如果树不是唯一的,则只需输出其中之一即可。

输入规格:

每个输入文件包含一个测试用例。对于每种情况,第一行给出一个正整数N(<= 30),即二叉树中节点的总数。第二行给出了前序顺序,第三行给出了后序顺序。一行中的所有数字都用空格分隔。

输出规格:

对于每个测试用例,如果树是唯一的,则在行“是”中首先打印printf,否则,则在“否”中打印。然后在下一行中打印相应的二叉树的有序遍历序列。如果解决方案不是唯一的,那么任何答案都可以。确保至少存在一种解决方案。一行中的所有数字必须完全由一个空格分隔,并且行尾不得有多余的空格。

思路:
本文参考柳神思路

#include<bits/stdc++.h>
using namespace std;

int n;
vector<int> in, pre, post;	//前中后序存储 
bool flag = true;	//标记是否唯一 

void reverse(int preLeft, int preRight, int postLeft, int postRight) {
	//
	if(preLeft == preRight) {
		in.push_back(pre[preLeft]);
		return ;
	}	
	
	if(pre[preLeft] == post[postRight]) {
		int i = preLeft + 1;
		while(i <= preRight && pre[i] != post[postRight - 1]) i++;
		if(i - preLeft > 1) {
			reverse(preLeft + 1, i - 1, postLeft, postLeft + (i - preLeft - 1) - 1);	
		} else {
			flag = false;
		}
		in.push_back(post[postRight]);
		reverse(i, preRight, postLeft + (i - preLeft - 1), postRight - 1);
	}
}

void init() {
	cin >> n;
	pre.resize(n), post.resize(n);
	for(int i = 0; i < n; i++) {
		cin >> pre[i];
	}
	for(int i = 0; i < n; i++) {
		cin >> post[i];
	}
}

int main() {
	init();
	reverse(0, n - 1, 0, n - 1);
	printf("%s\n%d", flag == true ? "Yes" : "No", in[0]);
    for (int i = 1; i < in.size(); i++)
        printf(" %d", in[i]);
    printf("\n");
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值