前序和后序什么时候可以确定一棵树

一般情况下,已知二叉树的前序和后序是无法唯一确定一颗二叉树的,因为可能存在多种情况,即一个结点可能是根的左孩子也有可能是根的右孩子。前序的开始的第一个与后序的最后一个是相等的,这个结点就是根结点,以后序的根结点的前面一个结点作为参考,寻找这个结点在前序中的位置,如果这个位置的前一个位置的节点不是上一次遍历的前序序列中第一个起始节点,则部分唯一。递归进行下去,如果直到递归结束,上述条件持续成立,则该树唯一确定。反之不唯一。

参考源

//1119. Pre - and Post - order Traversals(30)
#include <cstdio>  
#include <vector>  
using namespace std;
vector<int> ans;
int *pre, *post, unique = 1;

int findFromPre(int x, int l, int r) {
	for (int i = l; i <= r; i++) {
		if (x == pre[i]) {
			return i;
		}
	}
	return -1;
}

void setIn(int prel, int prer, int postl, int postr) {
	if (prel == prer) {
		ans.push_back(pre[prel]);
		return;
	}
	if (pre[prel] == post[postr]) {
		int x = findFromPre(post[postr - 1], prel + 1, prer);
		if (x - prel > 1) {
			setIn(prel + 1, x - 1, postl, postl + x - prel - 2);
			ans.push_back(post[postr]);
			setIn(x, prer, postl + x - prel - 2 + 1, postr - 1);
		}
		else {
			unique = 0;
			ans.push_back(post[postr]);
			setIn(x, prer, postl + x - prel - 2 + 1, postr - 1);
		}
	}
}

int main() {
	int n = 0;
	scanf("%d", &n);
	pre = new int[n];
	post = new int[n];
	for (int i = 0; i < n; i++) {
		scanf("%d", &pre[i]);
	}
	for (int i = 0; i < n; i++) {
		scanf("%d", &post[i]);
	}
	setIn(0, n - 1, 0, n - 1);
	printf("%s\n", unique ? "Yes" : "No");
	printf("%d", ans[0]);
	for (int i = 1; i < ans.size(); i++) {
		printf(" %d", ans[i]);
	}
	printf("\n");
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值