• 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:遍历输入的序列,找到第一个位于两个数之间的数(BST的性质)
    代码:
     1 #include <iostream>
     2 #include <vector>
     3 #include <map>
     4 using namespace std;
     5 map<int, bool> mp;
     6 int main() {
     7     int m, n, u, v, a;
     8     scanf("%d %d", &m, &n);
     9     vector<int> pre(n);
    10     for (int i = 0; i < n; i++) {
    11         scanf("%d", &pre[i]);
    12         mp[pre[i]] = true;
    13     }
    14     for (int i = 0; i < m; i++) {
    15         scanf("%d %d", &u, &v);
    16         for(int j = 0; j < n; j++) {
    17             a = pre[j];
    18             if ((a >= u && a <= v) || (a >= v && a <= u)) break;
    19         } 
    20         if (mp[u] == false && mp[v] == false)
    21             printf("ERROR: %d and %d are not found.
    ", u, v);
    22         else if (mp[u] == false || mp[v] == false)
    23             printf("ERROR: %d is not found.
    ", mp[u] == false ? u : v);
    24         else if (a == u || a == v)
    25             printf("%d is an ancestor of %d.
    ", a, a == u ? v : u);
    26         else
    27             printf("LCA of %d and %d is %d.
    ", u, v, a);
    28     }
    29     return 0;
    30 }
    31 //by liuchuo

    法2:

    由于前序序列排序之后是中序序列,所以可以根据前序和中序递归求LCA

    代码:

     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 const int maxn = 1e5 + 10;
     4 int pre[maxn], in[maxn];
     5 int prenum[maxn], innum[maxn];
     6 map<int, int> vis;
     7 int n, m;
     8 int a, b;
     9 
    10 int searchroot(int l, int r, int ll, int rr)
    11 {
    12     int rootpos = innum[pre[l]];
    13     int posa = innum[a], posb = innum[b];
    14     if ((rootpos - posa) * (rootpos - posb) <= 0)
    15         return pre[l];
    16     if (rootpos > posa)
    17         return searchroot(l + 1, l + rootpos - ll, ll, rootpos - 1);
    18     else return searchroot(l + rootpos - ll + 1, r, rootpos + 1, rr);
    19 }
    20 
    21 int main()
    22 {
    23     cin >> m >> n;
    24     for (int i = 1; i <= n; i++)
    25         scanf("%d", pre + i), vis[pre[i]] = 1, prenum[pre[i]] = i, in[i] = pre[i];
    26     sort(in + 1, in + 1 + n);
    27     for (int i = 1; i <= n; i++)
    28         innum[in[i]] = i;
    29     while (m--)
    30     {
    31         cin >> a >> b;
    32         if (!vis[a] && !vis[b]) printf("ERROR: %d and %d are not found.
    ", a, b);
    33         else if (!vis[a]) printf("ERROR: %d is not found.
    ", a);
    34         else if (!vis[b]) printf("ERROR: %d is not found.
    ", b);
    35         else
    36         {
    37             int res = searchroot(1, n, 1, n);
    38             if (res == a)
    39                 printf("%d is an ancestor of %d.
    ", a, b);
    40             else if (res == b)
    41                 printf("%d is an ancestor of %d.
    ", b, a);
    42             else printf("LCA of %d and %d is %d.
    ", a, b, res);
    43         }
    44     }
    45 }
  • 相关阅读:
    记忆力:记忆分类
    范例-项目-.NET-PetShop-4.0-数据库设计:MSPetShop4Service
    范例-项目-.NET-PetShop-4.0-数据库设计:MSPetShop4Profile
    范例-项目-.NET-PetShop-4.0-数据库设计:MSPetShop4Order
    范例-项目-.NET-PetShop-4.0-数据库设计:MSPetShop4
    范例-项目-.NET-PetShop-4.0-架构设计:PetShop 4.0架构与技术分析(2)
    范例-项目-.NET-PetShop-4.0-架构设计与技术分析:PetShop 4.0架构与技术分析(1)
    Error-Javascript:Uncaught Error: Malformed UTF-8 data at Object.stringify (crypto-js.js:478) at WordArray.init.toString (crypto-js.js:215)
    System.Diagnostics.Stopwatch.cs
    开发软件-服务器:Apache Tomcat
  • 原文地址:https://www.cnblogs.com/liuwenhan/p/11867992.html
Copyright © 2020-2023  润新知