PAT A1143 Lowest Common Ancestor (30分)BST的最近共同祖先

  • 一开始想到在BST中查找结点,记录查找路径,然后比较两个结点a和b的查找路径,路径中最后一个相同的结点就是最近共同祖先。于是写出了下面的代码,但是中间两个测试点运行超时。
  • 超时的原因估计是建树和查找的过程耗费时间。
#include <iostream>
#include <vector>
using namespace std;
const int maxn = 10000010;
bool exist[maxn] = {false};
//BST的结点定义
struct node{
	int data;
	node *lchild, *rchild;
};

//BST的插入函数,用于建立BST
void insert(node* &root, int x){
	if(root == NULL){
		root = new node;
		root->data = x;
		root->lchild = NULL;
		root->rchild = NULL;
		return;
	}
	if(root->data > x){
		insert(root->lchild, x);
	}
	else{
		insert(root->rchild, x);
	}
}
//BST查找结点,记录路径,在两个路径中寻找最后一个相同结点,就是他们的最近共同祖先
vector<int> path;
void search(node* root, int x){
	path.push_back(root->data);
	if(root->data == x){
		return;
	}
	else if(root->data > x){
		search(root->lchild, x);
	}
	else
		search(root->rchild, x);
}

int main(){
	int m, n;
	scanf("%d%d", &m, &n);
	node* root = NULL;
	for(int i = 0; i < n; i++){
		int x;
		scanf("%d", &x);
		exist[x] = true;
		insert(root, x);
	}
	for(int i = 0; i < m; i++){
		int a, b;
		scanf("%d%d", &a, &b);
		if(!exist[a] && !exist[b]){
			printf("ERROR: %d and %d are not found.\n", a, b);
		}
		else if(!exist[a]){
			printf("ERROR: %d is not found.\n", a);
		}
		else if(!exist[b]){
			printf("ERROR: %d is not found.\n", b);
		}
		else{
			vector<int> path1, path2;
			path.clear();
			search(root, a);
			path1 = path;
			path.clear();
			search(root, b);
			path2 = path;
			int j = 0, k = 0;
			int ans;
			while(j < path1.size() && k < path2.size()){
				if(path1[j] == path2[k])
					ans = path1[j];
				else
					break;
				j++;
				k++;
			}
			if(ans == a){
				printf("%d is an ancestor of %d.\n", a, b);
			}
			else if(ans == b){
				printf("%d is an ancestor of %d.\n", b, a);
			}
			else{
				printf("LCA of %d and %d is %d.\n", a, b, ans);
			}
		}
	}
	return 0;
}

  • 然后看了柳神的代码,恍然大悟。原来如此简单:
  • 由于是BST树,所以两个结点a和b的最近共同祖先的值一定>=a并且<=b。如果取=,说明a和b其中的一个结点就是另一个结点的父亲。
  • 题目正好给出的BST的先根序列,我们遍历先根序列,找到的第一个满足上面条件的结点就是答案。
  • 代码如下:
#include <iostream>
#include <map>
using namespace std;
const int maxn = 10010;
map<int, bool> mp;
int pre[maxn];

int main(){
	int m, n;
	cin >> m >> n;
	for(int i = 0; i < n; i++){
		scanf("%d", &pre[i]);
		mp[pre[i]] = true;
	}
	for(int i = 0; i < m; i++){
		int a, b;
		scanf("%d%d", &a, &b);
		if(mp[a] == false && mp[b] == false){
			printf("ERROR: %d and %d are not found.\n", a, b);
		}
		else if(mp[a] == false || mp[b] == false){
			printf("ERROR: %d is not found.\n", mp[a] == false ? a : b);
		}
		else{
			int ans;
			for(int j = 0; j < n; j++){
				ans = pre[j];
				if((ans >= a && ans <= b) || (ans >= b && ans <= a))
					break;
			}
			if(ans == a || ans == b){
				printf("%d is an ancestor of %d.\n", ans, ans == a ? b : a);
			}
			else{
				printf("LCA of %d and %d is %d.\n", a, b, ans);
			}
		}
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值