PAT 甲级 1143 Lowest Common Ancestor (30 分) 二刷

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 (≤ 1,000), the number of pairs of nodes to be tested; and N (≤ 10,000), 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、判断给出的两个数a和b是否在这棵树中。方式一,遍历数组。方式二,使用visit来存储某个数是否出现过,因为这道题的每一个数的范围是int,因此需要使用2个甚至4个来存储。visit_1存储非负数,visit_2存储负数,又由于数组的大小有限,因此可以使用两个数组组合的方式。
这道题这两种方式都行,第一种方法在一些情况下可能会超时,但是这道题没有。
2、搜寻两个数a和b在数中的位置,因为是二叉搜索树,可以根据根结点的大小,判断两个数在左子树还是在右子树。
有意思的是,我在寻找当前树左子树和右子树的分割点时,采用正向寻找,case 4超时,采用逆向查找,通过

#include <iostream>
#include <cstdio>
using namespace std;
int M,N;
int arr[100005];
bool visit_1[0xfffff]={false},visit_2[0xfffff]={false};
bool contain(int a)
{
/*    if(a>0)
        return visit_1[a];
    else
        return visit_2[-a];
*/
        for(int i=0;i<N;i++)
            if(arr[i]==a)
            return true;
        return false;
}
void LCA(int a,int b,int l,int r)
{
    int i;
    for(i=r;i>=l+1;i--)
        if(arr[i]<arr[l])
            break;
    i++;
    if(arr[l]==a)
        printf("%d is an ancestor of %d.\n",a,b);
    else if(arr[l]==b)
        printf("%d is an ancestor of %d.\n",b,a);
    else if(arr[l]>a&&arr[l]>b)
        LCA(a,b,l+1,i-1); //左子树
    else if(arr[l]<a&&arr[l]<b)
        LCA(a,b,i,r); //右子树
    else if(arr[l]>a&&arr[l]<b || arr[l]<a&&arr[l]>b )
        printf("LCA of %d and %d is %d.\n",a,b,arr[l]);
}
int main()
{
    cin>>M>>N;
    int a,b;
    for(int i=0;i<N;i++)
    {
        scanf("%d",&arr[i]);
        if(arr[i]>=0)
            visit_1[arr[i]]=true;
        else
            visit_2[-arr[i]]=true;
    }
    for(int i=0;i<M;i++)
    {
        scanf("%d%d",&a,&b);
        bool f1,f2;
        f1=contain(a);
        f2=contain(b);
        if(!f1||!f2)
        {
            if(!f1&&!f2)
                printf("ERROR: %d and %d are not found.\n",a,b);
            else if(!f1)
                printf("ERROR: %d is not found.\n",a);
            else
                printf("ERROR: %d is not found.\n",b);
        }
        else{
            LCA(a,b,0,N-1);
        }
    }
}

发布了174 篇原创文章 · 获赞 18 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_41173604/article/details/100538429