• A1151 LCA in a Binary Tree(30分)PAT 甲级(Advanced Level) Practice(C++)满分题解【LCA+树】


    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.

    代码如下:

    1. #include
    2. using namespace std;
    3. int n, m;//m个查询,n个节点
    4. vector<int> preorder, inorder;
    5. map<int,int> pos;
    6. void LCA(int inL, int inR, int preRoot, int a, int b)
    7. {
    8. if (inL > inR)return;
    9. int inRoot = pos[preorder[preRoot]], aIn = pos[a], bIn = pos[b];//根节点和ab在中序序列中的位置索引
    10. if (aIn < inRoot && bIn < inRoot)//都在左子树
    11. LCA(inL, inRoot - 1, preRoot + 1, a, b);
    12. else if (aIn > inRoot&& bIn > inRoot)//都在右子树
    13. LCA(inRoot + 1, inR, preRoot + 1 + (inRoot - inL), a, b);
    14. else if (aIn > inRoot&& bIn < inRoot || aIn < inRoot && bIn > inRoot)//分布在左右子树,当前即为LCA
    15. cout << "LCA of " << a << " and " << b << " is " << inorder[inRoot] << "." << endl;
    16. else if (aIn == inRoot)
    17. cout << a << " is an ancestor of " << b << "." << endl;
    18. else if (bIn == inRoot)
    19. cout << b << " is an ancestor of " << a << "." << endl;
    20. }
    21. int main()
    22. {
    23. cin >> m >> n;
    24. inorder.resize(n + 1);
    25. preorder.resize(n + 1);
    26. for (int i = 1; i <= n; i++) {
    27. cin >> inorder[i];
    28. pos[inorder[i]] = i;
    29. }
    30. for (int i = 1; i <= n; i++)
    31. cin >> preorder[i];
    32. int u, v;
    33. while (m--) {
    34. cin >> u >> v;
    35. if (pos[u] == 0 && pos[v] == 0)cout << "ERROR: " << u << " and " << v << " are not found." << endl;
    36. else if (pos[u] == 0 && pos[v] != 0)cout << "ERROR: " << u << " is not found." << endl;
    37. else if (pos[u] != 0 && pos[v] == 0)cout << "ERROR: " << v << " is not found." << endl;
    38. else {
    39. LCA(1, n, 1, u, v);
    40. }
    41. }
    42. return 0;
    43. }

    运行结果如下:

     

  • 相关阅读:
    Cmake输出git内容方式
    【Linux开发实用篇】Webmin和宝塔
    我们该如何运营Facebook账号呢?
    dubbo3.0.5同一个providerAPI的消费组,不同应用的消费者设置不同的消费者参数,会导致其它消费者服务无法启动
    LARGE LANGUAGE MODELS AS OPTIMIZERS
    Python 获取北上广深历史天气数据并做数据可视化
    阿里提出MS-Diffusion:一键合成你喜爱的所有图像元素,个性化生成新思路!
    SSM整合过程梳理
    【数学】旋转后仍为函数图像问题
    进入IT行业:选择前端开发还是后端开发?
  • 原文地址:https://blog.csdn.net/qq_47677800/article/details/126588636