PAT甲级 1143 最低公共祖先

原题链接

树中两个结点 U 和 V 的最低公共祖先(LCA)是指同时具有 U 和 V 作为后代的最深结点。

二叉搜索树 (BST) 递归定义为具有以下属性的二叉树:

若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值
若它的右子树不空,则右子树上所有结点的值均大于或等于它的根结点的值
它的左、右子树也分别为二叉搜索树
现在给定 BST 中的任意两个结点,请你找出它们的最低公共祖先。

输入格式
第一行包含两个整数 M 和 N,分别表示询问结点对数以及二叉搜索树中的结点数量。

第二行包含 N 个不同整数,表示该二叉搜索树的前序遍历序列。

接下来 M 行,每行包含两个整数 U 和 V,表示一组询问。

所有结点权值均在 int 范围内。

输出格式
对于每对给定的 U 和 V,输出一行结果。

如果 U 和 V 的 LCA 是 A,且 A 不是 U 或 V,则输出 LCA of U and V is A.。

如果 U 和 V 的 LCA 是 A,且 A 是 U 或 V 中的一个,则输出 X is an ancestor of Y.,其中 X 表示 A,Y 表示另一个结点。

如果 U 或 V 没有在 BST 中找到,则输出 ERROR: U is not found. 或 ERROR: V is not found. 或 ERROR: U and V are not found.。

数据范围
1≤M≤1000,
1≤N≤10000
输入样例:
6 8
6 3 1 2 5 4 8 7
2 5
8 7
1 9
12 -3
0 8
99 99
输出样例:
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 <bits/stdc++.h>
using namespace std;
const int N = 1000010;
int m, n;
int pre[N], in[N], f[N], dep[N];
unordered_map <int, int> pos;

int build(int il, int ir, int pl, int pr, int d){
    int root = pre[pl];
    int k = pos[root];
    dep[root] = d;
    
    if(il < k) f[build(il, k - 1, pl + 1, pl + 1 + k - 1 - il, d + 1)] = root;
    if(k < ir) f[build(k + 1, ir, pl + 1 + k - il, pr, d + 1)] = root;
    
    return root;
}
int main(){
    cin >> m >> n;
    for(int i = 0; i < n; i ++ ){
        cin >> pre[i];
        in[i] = pre[i];
    }
    
    sort(in, in + n);
    
    for(int i = 0; i < n; i ++ ){
        pos[in[i]] = i;
    }
    
    int d = 0;
    build(0, n - 1, 0, n - 1, d);
    
    while(m -- ){
        int u, v;
        cin >> u >> v;
        if(pos.count(u) && pos.count(v)){
            int a = u, b = v;
            
            while(a != b){
                if(dep[a] > dep[b]) a = f[a];
                else b = f[b];
            }
            if(a != u && a != v) printf("LCA of %d and %d is %d.\n", u, v, a);
            else if(u == a) printf("%d is an ancestor of %d.\n", a, v);
            else printf("%d is an ancestor of %d.\n", a, u);
            
        }
        else if(pos.count(u) == 0 && pos.count(v) == 0) 
            printf("ERROR: %d and %d are not found.\n", u, v);
        else if(pos.count(u) == 0)
            printf("ERROR: %d is not found.\n", u);
        else if(pos.count(v) == 0)
            printf("ERROR: %d is not found.\n", v);
    }

    return 0;
}

收获:

二叉树建树,并非一定要把树完全建立出来,根据需要的条件,可以建立 f [ ] ->保存每个节点的父节点,也可以建立 l [ ] r[ ] ->保存每个节点的左右孩子。(多叉树同样适用)

猜你喜欢

转载自blog.csdn.net/weixin_45660485/article/details/126085729