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

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

Simple 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

思路

本题是根据前序遍历结果和后续遍历结果来创建一棵二叉树,由于光有前序遍历和后续遍历并不能唯一确定一棵树,所以难点在于对于树唯一性的判断,即在什么情况下,由前序遍历和后序遍历能唯一确定一棵树。反过来想,什么时候不是唯一的呢?在后序遍历中,如果确定了一个根节点的位置,那么排在它前面的节点则是它的子树,包括左子树和右子树,则这段子树序列可以进一步根据前序遍历结果确定下一个根节点,如果这个根节点跟上一个根节点是挨在一起的,也就是它们中间没有其他节点,说明这个根节点是没有兄弟节点的,上个根节点只有一个孩子,那么这个根节点可以是它父节点的左孩子也可以是右孩子,这个时候便是不唯一了。那么其他情况下则是唯一的。光用文字描述可能有点。。。难懂。举个栗子说明一下,比如测试用例中的这个序列:

前序遍历:1 2 3 4 6 7 5

后续遍历:2 6 7 4 5 3 1

以前序遍历的序列为基准确定每个子树的根节点。我们可以看出1是这棵树的根节点,那么根据后序遍历序列,我们可以知道,在这个根节点之前的节点是它的子树节点,即2 6 7 4 5 3都是它的子树节点。前序遍历序列再往前走一个,可以知道1的一个孩子是2,它在后序遍历序列中排在1的子树节点的第一个,前面没有任何节点了,那么说明2这个节点没有任何孩子。而它和根节点1之间还是有序列的,那么说明它是有右兄弟的,不然不可能有节点序列排在它后面,这也唯一确定了2是根节点1的左孩子。那么根据前序遍历再往前一个节点我们可以知道3是1的右子树的根节点,那么介于2和3之间的序列则都是3的子树节点了。然后再依据此规则继续走下去。

根据上面分析的唯一确定节点位置的规则就可以很容易想到不唯一的情况,再看这个序列:
前序遍历:1 2 3 4 6 7 5

后序遍历:6 7 4 5 3 2 1

1是根节点,2是1的孩子,而在后序遍历的序列中,2的位置紧挨着1,2跟1之间没有其他序列,说明2节点是没有兄弟节点的,因为如果有兄弟节点,假设2是左孩子,它有右兄弟,那么在2和1之间肯定还有其他节点序列,假设2是右孩子,它有左兄弟,那么在前序遍历中,2跟1之间不可能没有其他序列了,因为前序遍历是先根再左再右,所以一个节点有左兄弟,那么它前面肯定还有序列的。由于2没有兄弟节点,说明1只有一个孩子,这个时候光凭前序遍历和后序遍历无法确定2究竟是左孩子还是右孩子,故这个时候树是不唯一的。

本题还有一个输出坑点,就是第二行输出之后必须打印一个换行符,不然会输出格式错误

AC代码:

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <cmath>

#define INT_MAX 2147483647

using namespace std;

typedef struct TreeNode {
	int val;
	TreeNode *left, *right;
}TreeNode, *Tree;

int preOrder[32], postOrder[32];
int site = 0, num = 0, n;
bool isUniq = true;

void Build(Tree &node, int s, int e) {
	if (s > e) {
		return;
	}
	node = (Tree)malloc(sizeof(TreeNode));
	node->val = postOrder[e];//后续遍历的最后一个节点肯定是当前子树的根节点
	node->left = node->right = NULL;
	site++;
	int child = s;
	for (; child < e; child++) {//寻找当前根节点的一个孩子
		if (postOrder[child] == preOrder[site])break;
	}
	if (e - child == 1) {//当前根节点只有一个孩子,此时树不唯一
		isUniq = false;
        Build(node->left, s, e - 1);
	}
	else if (e - child > 1) {//当前根节点有俩个孩子,且这个孩子定是左孩子
		Build(node->left, s, child);
		Build(node->right, child + 1, e - 1);
	}
}

void inOrder(Tree node) {
	if (node != NULL) {
		inOrder(node->left);
    cout << node->val;
    num++;
    if(num < n){
      cout << " ";
    }else{
      cout << endl;
    }
		inOrder(node->right);
	}
}

void deleteTree(Tree &root) {
	if (root != NULL) {
		deleteTree(root->left);
		deleteTree(root->right);
		free(root);
	}
}

int main() {
	cin >> n;
	for (int i = 0; i < n; i++) {
		cin >> preOrder[i];
	}
	for (int i = 0; i < n; i++) {
		cin >> postOrder[i];
	}
	Tree root = NULL;
	Build(root, 0, n - 1);
	cout << (isUniq ? "Yes" : "No") << endl;
	inOrder(root);
	deleteTree(root);
	
	system("pause");
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值