【PAT】1143 Lowest Common Ancestor(测试点分析,普通思想,建树利用排序树的特点找公共祖先)

【PAT】1143 Lowest Common Ancestor

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.

我的思想(绝大部分人的第一种思想)

1、利用前序遍历序列和中序遍历序列进行建树。其中中序遍历就是前序遍历的从小到大的排序(排序树的特点)。
2、然后开始前序遍历(假设需要找u,v两点的祖先)可以分为下面几个情况
①、u和v在root的两侧,也就是一个大于root一个小于root那么root就是他们的共同祖先。
②、若u,v都小于root那么进行遍历root的左子树,反之遍历其右子树。
③、将找到的key和原始的u,v进行比较,从而得出输出的情况。

注意:我们需要注意Key的取址范围是int 也就是 (1>>32,1<<31-1),所以大家可以考虑使用unordered_map,否则可能存在测试点段错误

AC代码:

#include<bits/stdc++.h>
#include<unordered_map>
using namespace std;
struct BNode {	
	int left, right;
};
unordered_map<int,BNode> BSTree;
unordered_map<int, bool>visited;
int pre[10007], in[10007];
int M, N;
int Build_Tree(int l1,int h1,int l2,int h2) {
	if (l1 > h1) return 0;
	int root = pre[l1];
	int m = l2;
	while (in[m] != root) m++;
	int len = m - l2;
	BSTree[root].left = Build_Tree(l1+1,l1+len,l2,m-1);
	BSTree[root].right = Build_Tree(l1+len+1,h1, m + 1, h2);
	return root;
}
int preOrder(int root,int a,int b) {
	if (root == 0) return 0;
	if ((a <= root && b >= root) || (a >= root && b <= root)) return root;
	if(a<root&&b<root)//a,b都在左子树上
		return preOrder(BSTree[root].left,a,b);
	if(a>root&&b>root)//a,b都在右子树上
		return preOrder(BSTree[root].right,a,b);
}
int main() {
	for (int i = 0; i < N; i++) {
		int temp; scanf("%d", &temp);
		pre[i] = in[i] = temp;
		visited[temp] = true;
	}
	sort(in, in + N);
	Build_Tree(0, N - 1, 0, N - 1);
	for (int i = 0; i < M; i++) {
		int a, b; scanf("%d %d", &a, &b);
		if (visited[a] == false && visited[b] == false)
		{printf("ERROR: %d and %d are not found.\n", a, b); continue;}
		if (visited[a] == false && visited[b] == true)
		{printf("ERROR: %d is not found.\n",a); continue;}
		if (visited[a] == true && visited[b] == false)
		{printf("ERROR: %d is not found.\n",b); continue; }
		int ansc = preOrder(pre[0], a, b);
		if (ansc == a) printf("%d is an ancestor of %d.\n",a,b);
		else if (ansc == b) printf("%d is an ancestor of %d.\n", b, a);
		else printf("LCA of %d and %d is %d.\n", a, b, ansc);
	}
}

猜你喜欢

转载自blog.csdn.net/qq_39072627/article/details/107399862