*PAT甲级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

题目大意

给定一棵树的前序后序序列,判断这两个序列能否唯一的确定一棵树,如果能则输出Yes然后输出树的中序序列,否则输出No,然后输出其中一个中序序列

思路

参考了1119 Pre- and Post-order Traversals (30 分)二叉树:已知前序序列与后序序列建树
大概就是如果树上有父亲结点只包含一个子节点,那么通过前序和后序无法唯一的构造这棵树,比如
在这里插入图片描述
在这里插入图片描述
这两棵树的前序都是12345,后序都是35421,然而这是两棵不同的树。但是如果规定树上父亲结点都有两个子结点,那就可以通过前序和后序唯一的构造它,比如:
在这里插入图片描述

#include <iostream>
#include <vector>
#include <cstdlib>
#include <algorithm>
using namespace std;
struct node
{
	int val;
	node* left;
	node* right;
	node(int v) : val(v), left(NULL), right(NULL) {}
};
int N;
bool isUnique = true;
vector<int> pre, post, in;
int pos[10001];
node* build(int preL, int preR, int postL, int postR);
void inOrder(node* root);
int main()
{
	cin >> N;
	for (int i = 0; i < 2; i++)
	{
		for (int j = 0; j < N; j++)
		{
			int node;
			cin >> node;
			if (i == 0)
				pre.push_back(node);
			else
			{
				post.push_back(node);
				pos[node] = j;
				//pos记录后序遍历序列中一个点前面有多少点
			}
		}
	}
	node* root = build(0, N - 1, 0, N - 1);
	if (isUnique)
		cout << "Yes" << endl;
	else
		cout << "No" << endl;
	inOrder(root);
	for (int i = 0; i < in.size(); i++)
	{
		if (i != 0)
			cout << " ";
		cout << in[i];
	}
	cout << endl;
	system("pause");
	return 0;
}
node* build(int preL, int preR, int postL, int postR)
{
	//preL == preR说明这棵子树的前序序列就自己一个结点
	//说明自己是叶子结点
	if (preL == preR)
	{
		node* root = new node(pre[preL]);
		root->left = root->right = NULL;
		return root;
	}
	else if (preL > preR || postL > postR)
		return NULL;
		
	//前序的首结点和后序的末结点相同说明是子树的根结点
	if (pre[preL] == post[postR])
	{
		node* root = new node(pre[preL]);
		int numChild = pos[post[postR]] - postL;
		//numChild表示以pre[preL]为根的树一共有多少孩子(不包括根自己)
		//如果只有一个孩子说明不能唯一构造二叉树,因为要求每个父结点都要两个子结点
		
		if (numChild == 1)
			isUnique = false;
			
		//pre[preL+1]表示pre[preL]的一个子结点(优先左孩子,如果没有才是右孩子)
		//pos[pre[preL+1]]表示以这个子结点为根的树有多少孩子(如果pre[preL+1]是右孩子,numLeft为0)
		int numLeft = pos[pre[preL + 1]] - postL;
		root->left = build(preL + 1, preL + 1 + numLeft, postL, postL + numLeft);
		root->right = build(preL + numLeft + 2, preR, postL + numLeft + 1, postR - 1);
		return root;
	}
}
void inOrder(node* root)
{
	if (root->left != NULL)
		inOrder(root->left);
	in.push_back(root->val);
	if (root->right != NULL)
		inOrder(root->right);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值