二、解题思路

这个题利用了二叉树孩子结点和其父节点下标的索引关系:若顺序树的索引从1开始,那么子节点的索引除以二就是其父节点的索引,注意一定是要使得树的索引从1开始才有这个性质

三、解题代码

#include <iostream>
#include <vector>
using namespace std;
int main()
{
   
    int n;
    cin >> n;
    auto T = new int[n];
    if(!T)	exit(0);
    for (int i = 0; i < n; i++)
        cin >> T[i];
    int s1, s2;
    cin >> s1 >> s2;

    if (!T[0])
    {
   
        cout << "ERROR: T[" << 1 << "] is NULL" << endl;
        exit(0);
    }
    if (!T[s1 - 1])
    {
   
        cout << "ERROR: T[" << s1 << "] is NULL" << endl;
        exit(0);
    }
    if (!T[s2 - 1])
    {
   
        cout << "ERROR: T[" << s2 << "] is NULL" << endl;
        exit(0);
    }

    vector<int> sub1, sub2;
    while (1)
    {
   
        sub1.push_back(s1);
        if (s1 == 1)
            break;
        s1 /= 2;
    }
    while (s2)
    {
   
        sub2.push_back(s2);
        if (s2 == 1)
            break;
        s2 /= 2;
    }
    int i, j;
    for (i = sub1.size() - 1, j = sub2.size() - 1; i >= 0 && j >= 0 && sub1[i] == sub2[j]; i--, j--)
        ;
    cout << sub1[i + 1] << ' ' << T[sub1[i + 1] - 1] << endl;
    delete[] T;
    T = nullptr;
    return 0;
}

四、运行结果