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、序列由三部分组成,第一个结点(根节点)、值小于第一个结点的结点(左子树)、值大于第一个结点的结点(右子树)

2、设置一个标记数组标记第一个结点的祖先,在查找第二个结点时,记录相同结点,即可得到最近公共结点。

#include <iostream>
#include <bits/stdc++.h>
using namespace std;
struct node{
    int v;
    int lc, rc;
}a[10010];
bool vis[10010];
int create(int l, int r){
    if(l>=r){
        return -1;
    }

    int i = l;
    while(i<r&&a[i].v<=a[l].v) i++;

    a[l].lc = create(l+1, i);;
    a[l].rc = create(i, r);
    return l;
}
void Search(int k1, int k2, int &x, int &y, int &ans, int n){
    int i = 0;
    while(i!=-1) {
        vis[i] = true;//标记祖先
        if(a[i].v == k1){
            x = i;
            break;
        }
        i = a[i].v > k1?a[i].lc : a[i].rc;
    }

    int j = 0;
    while(j!=-1) {
        if(vis[j]) ans = a[j].v;//记录公共祖先
        if(a[j].v == k2){
            y = j;
            break;
        }
        j = a[j].v > k2 ? a[j].lc : a[j].rc;
    }
}
int main(){

    int n, m;
    cin>>m>>n;
    for(int i=0; i<n; i++){
        cin>>a[i].v;
    }
    create(0, n);
    int u, v;
    while(m--){
        memset(vis, false, sizeof(vis));
        cin>>u>>v;
        int ans = -1, x = -1, y = -1;
        Search(u, v, x, y, ans, n);
        if(x == -1 && y == -1){
            printf("ERROR: %d and %d are not found.\n", u, v);
        } else if(x == -1){
            printf("ERROR: %d is not found.\n",u);
        } else if(y == -1){
            printf("ERROR: %d is not found.\n",v);
        } else if(ans == u){
            printf("%d is an ancestor of %d.\n", u, v);
        } else if(ans == v){
            printf("%d is an ancestor of %d.\n", v, u);
        } else{
            printf("LCA of %d and %d is %d.\n", u, v, ans);
        }
    }
    return 0;
}
发布了88 篇原创文章 · 获赞 7 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/Q_smell/article/details/81611059