1143. Lowest Common Ancestor (30) 最近公共祖先

1143. Lowest Common Ancestor (30)

时间限制
200 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

The lowest common ancestor (LCA) of two nodes U and V in a tree is the deepest node that has both U and V as descendants.

A binary search tree (BST) is recursively defined as a binary tree which has the following properties:

  • The left subtree of a node contains only nodes with keys less than the node's key.
  • The right subtree of a node contains only nodes with keys greater than or equal to the node's key.
  • Both the left and right subtrees must also be binary search trees.

Given any two nodes in a BST, you are supposed to find their LCA.

Input Specification:

Each input file contains one test case. For each case, the first line gives two positive integers: M (<= 1000), the number of pairs of nodes to be tested; and N (<= 10000), the number of keys in the BST, respectively. In the second line, N distinct integers are given as the preorder traversal sequence of the BST. Then M lines follow, each contains a pair of integer keys U and V. All the keys are in the range of int.

Output Specification:

For each given pair of U and V, print in a line "LCA of U and V is A." if the LCA is found and A is the key. But if A is one of U and V, print "X is an ancestor of Y." where X is A and Y is the other node. If U or V is not found in the BST, print in a line "ERROR: U is not found." or "ERROR: V is not found." or "ERROR: U and V are not found.".

Sample Input:
6 8
6 3 1 2 5 4 8 7
2 5
8 7
1 9
12 -3
0 8
99 99
Sample Output:
LCA of 2 and 5 is 3.
8 is an ancestor of 7.
ERROR: 9 is not found.
ERROR: 12 and -3 are not found.
ERROR: 0 is not found.
ERROR: 99 and 99 are not found.

        给出一个搜索二叉树的前序遍历序列,给出两个数字,试图找出这两个数字在二叉树中的最近公共祖先(LCA)结点。

        首先建立二叉树比较简单。然后通过findd函数去寻找最近公共祖先。findd函数有两个作用,f为1时是为了寻找a,找到了返回true,此时参数b用不到;f为0时是还在寻找两个点a和b的LCA,此时返回值用不到。当a和b一个比tree->data大另一个比tree->data小时,则当前节点的data就是a和b的LCA(如果a和b都在树中的话),所以接下来把f改为1继续调用findd函数来检查a和b是否都在二叉树中。


#include <cstdio>
#include <cstdlib>

using namespace std;

typedef struct node *NODE;
typedef struct node {
	int da;
	NODE l, r;
}node;

NODE T;
int M, N;
int pre[10001];

NODE buildtree(int s, int e) {
	if (e < s) return NULL;
	if (s == e) {
		NODE tnode = (NODE)malloc(sizeof(node));
		tnode->da = pre[s];
		tnode->l = NULL;
		tnode->r = NULL;
		return tnode;
	}
	int i = s + 1;
	while (i <= e && pre[i] < pre[s]) i++;
	NODE tnode = (NODE)malloc(sizeof(node));
	tnode->da = pre[s];
	tnode->l = buildtree(s + 1, i - 1);
	tnode->r = buildtree(i, e);
	return tnode;
}

bool findd(NODE tree, int a, int b,int f) {
	if (f == 1) {
		if (tree == NULL) return false;
		if (tree->da == a) return true;
		return findd(a < tree->da ? tree->l : tree->r, a, 0, 1);
	}
	else {
		if (tree == NULL) {
			printf("ERROR: %d and %d are not found.\n", a, b);
			return true;
		}
		if (a < tree->da&&b < tree->da) findd(tree->l, a, b, 0);
		else if (a > tree->da&&b > tree->da) findd(tree->r, a, b, 0);
		else {
			bool fda = tree->da == a || findd(tree->l, a, 0, 1) || findd(tree->r, a, 0, 1);
			bool fdb = tree->da == b || findd(tree->l, b, 0, 1) || findd(tree->r, b, 0, 1);
			if (fda&&fdb) {
				if (tree->da == a || tree->da == b) 
					printf("%d is an ancestor of %d.\n", tree->da == a ? a : b, tree->da == b ? a : b);
				else
					printf("LCA of %d and %d is %d.\n", a, b, tree->da);
			}
			else if (fda == false && fdb == false) {
				printf("ERROR: %d and %d are not found.\n", a, b);
			}
			else {
				printf("ERROR: %d is not found.\n", fda ? b : a);
			}
			return true;
		}
	}
}

int main() {
	scanf("%d %d", &M, &N);
	int a, b;
	for (int i = 0; i < N; i++) {
		scanf("%d", &pre[i]);
	}
	T = buildtree(0, N - 1);
	for (int i = 0; i < M; i++) {
		scanf("%d %d", &a, &b);
		findd(T, a, b, 0);
	}
	return 0;
}

原创文章 59 获赞 41 访问量 10万+

猜你喜欢

转载自blog.csdn.net/rzdyzx/article/details/79676320
今日推荐