Pre- and Post-order Traversals (30)

Pre- and Post-orderTraversals (30)

时间限制

400 ms

内存限制

65536 kB

Suppose that all the keys in a binary treeare distinct positive integers. A unique binary tree can be determined by agiven pair of postorder and inorder traversal sequences, or preorder andinorder traversal sequences. However, if only the postorder and preordertraversal sequences are given, the corresponding tree may no longer be unique.

Now given a pair of postorder and preordertraversal sequences, you are supposed to output the corresponding inordertraversal sequence of the tree. If the tree is not unique, simply output anyone of them.

Input Specification:

Each input file contains one test case. Foreach case, the first line gives a positive integer N (<=30), the total numberof nodes in the binary tree. The second line gives the preorder sequence andthe third line gives the postorder sequence. All the numbers in a line areseparated 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 inthe 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 atleast one solution exists. All the numbers in a line must be separated byexactly 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


问题分析:通过先序和后序确定二叉树然后写出中序,前序开始的第一个应该与后序的最后一个是相等的,那么就是根节点,然后根据这个来确定左右子树,前序从前往后看,后序从后往前看,前序与后序相同的一个应该是子树的根节点,然后这个相同的左右部分就是左右子树,依次递归建立二叉树。

拿样例举例:
1 (2) [3 {4 6 7} <5>]
(2) [{6 7 4} <5> 3] 1
不同的括号对应不同的子树区间
第一次递归划分了(2)-(2),[3 4 6 7 5]-[6 7 4 5 3]
由于(2)只有一棵,不继续划分。
第二次递归划分了{4 6 7}-{6 7 4},<5>-<5>

第三次递归划分了(6)-(6),(7)-(7)     --------------------->找到网上博客的例子


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

const int maxn = 35;
int preOrder[maxn];
int postOrder[maxn];
bool isUnique = true;
struct node{
	int left = - 1 ,right = -1;
}node[maxn];

void build(int prel,int prer,int postl,int postr)
{
	if (prel >= prer)
		return ;
	
	int parent =  prel;
	//前序遍历第二个为子树结点 
	int v = preOrder[prel+1];
	int postindex;
	for(int i = postl; i < postr; i++){
		if (v == postOrder[i]){
			postindex = i;
			break;
		}
	}
	
	int subnum = postindex - postl;	//以val为根节点的子树节点的个数
	//即以val为根节点的子树只有一个孩子,可以为左右任意孩子,不唯一
	if (prer-prel-1 == subnum){
		isUnique = false;
	} 
	
	node[parent].left = prel+1;
	build(prel+1,prel+subnum+1,postl,postindex);
	//以prel+1为根节点的子树小于parent的所有子树结点的个数,说明还有右孩子
	if (prer-prel-1 > subnum){
		node[parent].right = prel+subnum+2;
		build(prel+subnum+2,prer,postindex+1,postr-1);
	} 
}

bool first = true;
void inorder(int root)
{
	if (root == -1){
		return ;
	}
	
	inorder(node[root].left);
	if (first){
		first = false;
		cout << preOrder[root];
	}
	else{
		cout << " " << preOrder[root];
	} 
	inorder(node[root].right);
}

int main()
{
	int n;
	
	cin >> n;
	for(int i = 1; i <= n; i++)
		cin >> preOrder[i];	
	for(int i = 1; i <= n; i++)
		cin >> postOrder[i];
		
	build(1,n,1,n);
	
	if (isUnique)
		cout << "Yes" << endl; 
	else	
		cout << "No" << endl;
	
	inorder(1);
	cout << endl;
	return 0;
} 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值