还是3.18的考试,写题速度太慢了,最后一题建树之后没多少时间了。还有就是java写久了,指针全光了,并且对树这种结构不太熟练。没想好如何利用二叉查找树这种特殊的数据结构,其实要查u、v,对于某个节点node,如果node.key在u和v之间的话,该节点就是那个lowest common ancestor。
后来还有一个测试点没过,想了想是如果u==v的话,要按照这种格式输出But if A is one of U and V, print "X is an ancestor of Y."
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 99Sample 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.
#include <stdio.h>
#include <stack>
using namespace std;
struct Node{//树的节点结构
Node *left;
Node *right;
int key;
}Tree[10001];//静态数组
int loc;//静态数组中的元素个数
Node *create()//申请未使用的节点
{
Tree[loc].left=Tree[loc].right=NULL;
return &Tree[loc++];
}
Node* buildTree(int num,Node* root)//建树按照要求建,小于自身往左子树放,大于等于往右子树放
{
if(root==NULL)
{
Node* node=create();
node->key=num;
return node;
}
if(num<root->key)
{
root->left=buildTree(num,root->left);
}else
{
root->right=buildTree(num,root->right);
}
return root;
}
bool findU(int u,Node* root)//从root开始查找u
{
while(root!=NULL)
{
if(root->key==u)
return true;
else if(root->key<u)
root=root->right;
else
root=root->left;
}
return false;
}
void checkUV(int u,int v)
{
int bigger=u>v? u:v;
int smaller=u<v? u:v;
int aim=-1;
bool hasFindU=false;
bool hasFindV=false;
stack<Node*> tree;
tree.push(&Tree[0]);
Node* root=tree.top();
while (root != NULL) {
if (root->key >= smaller && root->key <= bigger) {//如果在两者之间就找到了,这个可能的解,再从这个点开始往下找u、v看他们存不存在
aim = root->key;
hasFindU = findU(u, root);
hasFindV = findU(v, root);
break;
} else if (root->key < smaller && root->key < bigger) {//如果u、v都比它大,再去右子树找
tree.push(root->right);
} else if (root->key > smaller && root->key > bigger) {//如果u、v都比它小,则去左子树找
tree.push(root->left);
}
root = tree.top();
}
if(!hasFindU&&!hasFindV)//按格式输出
{
printf("ERROR: %d and %d are not found.",u,v);
}
else if(hasFindU&&!hasFindV)
{
printf("ERROR: %d is not found.",v);
}
else if(!hasFindU&&hasFindV)
{
printf("ERROR: %d is not found.",u);
}
else
{
if(aim==u&&aim!=v)
{
printf("%d is an ancestor of %d.",u,v);
}
else if(aim==v&&aim!=u)
{
printf("%d is an ancestor of %d.",v,u);
}
else if(aim==v&&aim==u)
{
printf("%d is an ancestor of %d.",v,u);
}
else
{
printf("LCA of %d and %d is %d.",u,v,aim);
}
}
printf("\n");
}
int N,M;
int main()
{
while(scanf("%d%d",&M,&N)!=EOF){
int a;
Node *node=NULL;
for(int i=1;i<=N;i++)
{
scanf("%d",&a);
node=buildTree(a,node);
}
int u,v;
while(M--) {
scanf("%d%d", &u, &v);
checkUV(u, v);
}
}
return 0;
}