• PAT 1143 Lowest Common Ancestor


    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.

    总结:这是道题目其实挺简单的,主要考察的是对耗时的控制,所以点在于控制好时间复杂度

    唉,我只会常规的方法(先建立一颗二叉搜索数,然后遍历二叉搜索树,依据一定的方法找到对一个的位置即可)

    代码:

    1. #include
    2. #include
    3. using namespace std;
    4. struct node{
    5. int val;
    6. struct node *left,*right;
    7. };
    8. node *build(node *root,int v){
    9. if(root==NULL){
    10. root=new node();
    11. root->val=v;
    12. root->left=root->right=NULL;
    13. }
    14. else if(v<=root->val) root->left=build(root->left,v);
    15. else root->right=build(root->right,v);
    16. return root;
    17. }
    18. void dfs(node *root,int a,int b){
    19. if((a>root->val && bval) || (aval && b>root->val)){
    20. printf("LCA of %d and %d is %d.\n",a,b,root->val);
    21. return;
    22. }
    23. else if(a==root->val){
    24. printf("%d is an ancestor of %d.\n",a,b);
    25. return;
    26. }
    27. else if(b==root->val){
    28. printf("%d is an ancestor of %d.\n",b,a);
    29. return;
    30. }
    31. int t=max(a,b);
    32. if(root->val>t) dfs(root->left,a,b);
    33. if(root->valdfs(root->right,a,b);
    34. }
    35. int main(){
    36. int m,n;
    37. scanf("%d%d",&m,&n);
    38. int a[n];
    39. unordered_map<int,int> p;
    40. node *root=NULL;
    41. for(int i=0;i
    42. scanf("%d",&a[i]);
    43. p[a[i]]=1;
    44. root=build(root,a[i]);
    45. }
    46. for(int i=0;i
    47. int a,b;
    48. scanf("%d%d",&a,&b);
    49. if(!p[a] && !p[b]) printf("ERROR: %d and %d are not found.\n",a,b);
    50. else if(!p[a]) printf("ERROR: %d is not found.\n",a);
    51. else if(!p[b]) printf("ERROR: %d is not found.\n",b);
    52. else dfs(root,a,b);
    53. }
    54. return 0;
    55. }

    柳神代码:

    简洁明了,想法太妙了!

    1. #include
    2. #include
    3. using namespace std;
    4. int main(){
    5. int m,n;
    6. scanf("%d%d",&m,&n);
    7. int w[n];
    8. unordered_map<int,bool> p;
    9. for(int i=0;i
    10. scanf("%d",&w[i]);
    11. p[w[i]]=true;
    12. }
    13. for(int i=0;i
    14. int a,b,t;
    15. scanf("%d%d",&a,&b);
    16. for(int j=0;j
    17. t=w[j];
    18. if((t>a && tb && tbreak;
    19. }
    20. if(!p[a] && !p[b]) printf("ERROR: %d and %d are not found.\n",a,b);
    21. else if(!p[a] || !p[b]) printf("ERROR: %d is not found.\n",!p[a]?a:b);
    22. else if(t==a || t==b) printf("%d is an ancestor of %d.\n",t==a?a:b,t==a?b:a);
    23. else printf("LCA of %d and %d is %d.\n",a,b,t);
    24. }
    25. return 0;
    26. }

    2022.10.31  第一遍已经算是刷完了(接下来好好准备第二遍,明年3月份的考试!)

    好好学习,天天向上!

    我要考研!

  • 相关阅读:
    【Qt系列】QtableWidget表格列宽自适应表格大小
    QtCreator5.15.0编译全过程记录
    “奶爸车”成功了,那“女性车”呢?
    MES系统是怎样实现生产调度的?
    3. 内核解压-确定解压信息
    迅为瑞芯微RK3399开发板设置Buildroot文件系统测试MYSQL允许远程访问
    Python3入门教程||Python3 XML解析
    ubuntu http 服务器响应
    git克隆一直报错remote: HTTP Basic: Access denied
    彻底理解并解决服务器出现大量TIME_WAIT - 第二篇
  • 原文地址:https://blog.csdn.net/weixin_50679551/article/details/127624025