• 1143 Lowest Common Ancestor


    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:

    1. 6 8
    2. 6 3 1 2 5 4 8 7
    3. 2 5
    4. 8 7
    5. 1 9
    6. 12 -3
    7. 0 8
    8. 99 99

    Sample Output:

    1. LCA of 2 and 5 is 3.
    2. 8 is an ancestor of 7.
    3. ERROR: 9 is not found.
    4. ERROR: 12 and -3 are not found.
    5. ERROR: 0 is not found.
    6. ERROR: 99 and 99 are not found.

    自己写的后面三个测试点超时了,好像是递归建树这里超时,在这里记录一下思路:

    因为输入的是前序遍历,根据二叉搜索树定义,可以根据前序序列建树,同时记录每个节点的父节点,根节点的父节点为NULL,map记录出现的节点,输入u,v后先判断是否存在,如果都存在则通过r->fa找到父节点,并分别记录从u、v到达根节点的路径,然后将两条路径进行对比,第一个相同的值就是LCA。

    1. #include
    2. #include
    3. #include
    4. using namespace std;
    5. struct node {
    6. int val;
    7. node *fa;
    8. node *left;
    9. node *right;
    10. } *nu, *nv;
    11. int n, m, cnt1, cnt2, lca, u, v;
    12. int a[10010], t1[10010], t2[10010];
    13. bool flag1, flag2, find1, find2, flag;
    14. map<int, node *>q;
    15. node *build(node *r, node *fa, int val) {
    16. if (r == NULL) {
    17. r = new node();
    18. r->fa = fa;
    19. r->left = r->right = NULL;
    20. r->val = val;
    21. q[val] = r;
    22. } else if (val < r->val) {
    23. r->left = build(r->left, r, val);
    24. } else {
    25. r->right = build(r->right, r, val);
    26. }
    27. return r;
    28. }
    29. int main() {
    30. scanf("%d %d", &m, &n);
    31. node *root = NULL;
    32. for (int i = 0; i < n; i++) {
    33. scanf("%d", &a[i]);
    34. root = build(root, NULL, a[i]);
    35. }
    36. while (m--) {
    37. scanf("%d %d", &u, &v);
    38. flag = 0;
    39. flag1 = flag2 = 0;
    40. find1 = find2 = 0;
    41. cnt1 = cnt2 = 0;
    42. map<int, node *>::iterator it1, it2;
    43. it1 = q.find(u);
    44. it2 = q.find(v);
    45. if (q.find(u) != q.end()) {
    46. flag1 = 1;
    47. }
    48. if (q.find(v) != q.end()) {
    49. flag2 = 1;
    50. }
    51. if (!flag1 && !flag2) {
    52. printf("ERROR: %d and %d are not found.\n", u, v);
    53. } else if (!flag1 && flag2) {
    54. printf("ERROR: %d is not found.\n", u);
    55. } else if (flag1 && !flag2) {
    56. printf("ERROR: %d is not found.\n", v);
    57. } else {
    58. nu = it1->second;
    59. nv = it2->second;
    60. while (nu) {
    61. t1[cnt1++] = nu->val;
    62. nu = nu->fa;
    63. }
    64. while (nv) {
    65. t2[cnt2++] = nv->val;
    66. nv = nv->fa;
    67. }
    68. for (int i = 0; i < cnt1; i++) {
    69. if (flag) {
    70. break;
    71. }
    72. for (int j = 0; j < cnt2; j++) {
    73. if (t1[i] == t2[j]) {
    74. lca = t1[i];
    75. flag = 1;
    76. break;
    77. }
    78. }
    79. }
    80. if (u == lca) {
    81. printf("%d is an ancestor of %d.\n", u, v);
    82. } else if (v == lca) {
    83. printf("%d is an ancestor of %d.\n", v, u);
    84. } else {
    85. printf("LCA of %d and %d is %d.\n", u, v, lca);
    86. }
    87. }
    88. }
    89. return 0;
    90. }

    最后看的柳神的思路,不得不说简直是降维打击,太强了:PAT 1143. Lowest Common Ancestor (30) – 甲级_柳婼的博客-CSDN博客

  • 相关阅读:
    mongo数据同步的三种方案
    【世界历史】第一集——石器时代的人们
    当配置类用@Component不断调用@Bean注解的方法,会new出新对象,但是并不会覆盖一开始注入spring容器中的那个对象
    vscode 如何断点调试ros1工程
    不要小看一个Redis~ 从头到尾全是精华,阿里Redis速成笔记太香了
    FME——湘源规划用地CAD带指标导入ArcGIS数据库
    基于quartz的定时任务动态启停实现分析(人人平台为例)
    机器学习之前的环境准备
    苹果爆出台积电及三星3纳米制程良率远低于60% | 百能云芯
    HackTheBox MetaTwo 网站框架CVE获取用户shell和破解私钥提权
  • 原文地址:https://blog.csdn.net/weixin_53199925/article/details/128106269