• 1151 LCA in a Binary Tree


    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.

    Given any two nodes in a binary tree, 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 binary tree, respectively. In each of the following two lines, N distinct integers are given as the inorder and preorder traversal sequences of the binary tree, respectively. It is guaranteed that the binary tree can be uniquely determined by the input sequences. 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 binary tree, 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. 7 2 3 4 6 5 1 8
    3. 5 3 7 2 6 4 8 1
    4. 2 6
    5. 8 1
    6. 7 9
    7. 12 -3
    8. 0 8
    9. 99 99

    Sample Output:

    1. LCA of 2 and 6 is 3.
    2. 8 is an ancestor of 1.
    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.

    不需要建树,只需要记录每个值的父节点是谁即可。

    给定前序和中序序列,递归,map记录每个值父节点在前序序列中的下标,根节点的父节点的下标是-1,先判断所给的一对节点是否都存在,若存在,通过map来形成两个节点各自从该节点到根节点的值的序列,然后倒序遍历两个序列,当找到最后一个相同的值的时候,就是这两个节点的最低公共祖先LCA。

    1. #include
    2. #include
    3. #include
    4. using namespace std;
    5. int n, m, cnt1, cnt2, lca, u, v, nu, nv;
    6. int midorder[10010], preorder[10010], t1[10010], t2[10010];
    7. bool flag1, flag2;
    8. unordered_map<int, int>q;
    9. void build(int fa, int mid_l, int mid_r, int pre_l, int pre_r) {
    10. if (mid_l > mid_r || pre_l > pre_r) {
    11. return;
    12. }
    13. q[preorder[pre_l]] = fa;
    14. int t = mid_l;
    15. while (preorder[pre_l] != midorder[t]) {
    16. t++;
    17. }
    18. int len = t - mid_l;
    19. build(pre_l, mid_l, t - 1, pre_l + 1, pre_l + len);
    20. build(pre_l, t + 1, mid_r, pre_l + len + 1, pre_r);
    21. }
    22. int main() {
    23. scanf("%d %d", &m, &n);
    24. for (int i = 0; i < n; i++) {
    25. scanf("%d", &midorder[i]);
    26. }
    27. for (int i = 0; i < n; i++) {
    28. scanf("%d", &preorder[i]);
    29. }
    30. build(-1, 0, n - 1, 0, n - 1);
    31. unordered_map<int, int>::iterator it1, it2;
    32. while (m--) {
    33. scanf("%d %d", &u, &v);
    34. flag1 = flag2 = 0;
    35. cnt1 = cnt2 = 0;
    36. it1 = q.find(u);
    37. it2 = q.find(v);
    38. if (q.find(u) != q.end()) {
    39. flag1 = 1;
    40. }
    41. if (q.find(v) != q.end()) {
    42. flag2 = 1;
    43. }
    44. if (!flag1 && !flag2) {
    45. printf("ERROR: %d and %d are not found.\n", u, v);
    46. } else if (!flag1 && flag2) {
    47. printf("ERROR: %d is not found.\n", u);
    48. } else if (flag1 && !flag2) {
    49. printf("ERROR: %d is not found.\n", v);
    50. } else {
    51. nu = u, nv = v;
    52. while (true) {
    53. t1[cnt1++] = nu;
    54. if (q[nu] == -1) {
    55. break;
    56. }
    57. nu = preorder[q[nu]];
    58. }
    59. while (nv != -1) {
    60. t2[cnt2++] = nv;
    61. if (q[nv] == -1) {
    62. break;
    63. }
    64. nv = preorder[q[nv]];
    65. }
    66. for (int i = cnt1 - 1, j = cnt2 - 1; i >= 0 && j >= 0; i--, j--) {
    67. if (t1[i] == t2[j] && (i == 0 || j == 0 || t1[i - 1] != t2[j - 1])) {
    68. lca = t1[i];
    69. break;
    70. }
    71. }
    72. if (u == lca) {
    73. printf("%d is an ancestor of %d.\n", u, v);
    74. } else if (v == lca) {
    75. printf("%d is an ancestor of %d.\n", v, u);
    76. } else {
    77. printf("LCA of %d and %d is %d.\n", u, v, lca);
    78. }
    79. }
    80. }
    81. return 0;
    82. }
  • 相关阅读:
    springboot + vue实战
    【面试必刷TOP101】寻找峰值 & 数组中的逆序对
    Spring官方都推荐使用的@Transactional事务,为啥我不建议使用!
    用于演示文稿的新 Dapr 幻灯片
    【Java】UWB高精度工业定位系统项目源代码
    基于php的就业咨询网站
    豆瓣点评9.3分,10w好评的《python实战案例80个实例问答》,28天基础入门,学不会我退出IT界
    新特性(一)HTML5和CSS3新特性
    Aspose.Words 操作 Word 画 EChart 图
    阿里面试官:聊聊如何格式化Instant
  • 原文地址:https://blog.csdn.net/weixin_53199925/article/details/128180042