题目描述
给定一棵二叉搜索树的先序遍历序列,要求你找出任意两结点的最近公共祖先结点(简称 LCA)。
输入
输入的第一行给出两个正整数:待查询的结点对数 M(≤ 1 000)和二叉搜索树中结点个数 N(≤ 10 000)。随后一行给出 N 个不同的整数,为二叉搜索树的先序遍历序列。最后 M 行,每行给出一对整数键值 U 和 V。所有键值都在整型int范围内。
输出
对每一对给定的 U 和 V,如果找到 A
是它们的最近公共祖先结点的键值,则在一行中输出 LCA of U and V is A.
。但如果 U 和 V 中的一个结点是另一个结点的祖先,则在一行中输出 X is an ancestor of Y.
,其中 X
是那个祖先结点的键值,Y
是另一个键值。如果 二叉搜索树中找不到以 U 或 V 为键值的结点,则输出 ERROR: U is not found.
或者 ERROR: V is not found.
,或者 ERROR: U and V are not found.
。
输入样例1
6 8
6 3 1 2 5 4 8 7
2 5
8 7
1 9
12 -3
0 8
99 99
输出样例1
LCA of 2 and 5 is 3.
8 is an ancestor of 7.
ERROR: 9 is not found.
ERROR: 12 and -3 are not found.
ERROR: 0 is not found.
ERROR: 99 and 99 are not found.
最近公共祖先特点:
数据key1 key2 假设key1>key2,则
设祖先root的值为data,会有这样的特点:data<key1&&data>key2
所以可以从头节点开始遍历
我们要注意输入中的值存不存在 可以写一个search来判断:
bool search(int key)
{
node* temp = root;
while (key != temp->data)
{
if (key < temp->data)
{
temp = temp->left;
}
else {
temp = temp->right;
}
if (temp == nullptr)
{
return false;
}
}
return true;
}
完整代码如下:
#include <bits/stdc++.h>
using namespace std;
class node
{
public:
int data;
node* left, * right;
node() :data(0), left(nullptr), right(nullptr) {}
};
class tree
{
public:
node* root;
int num;
tree(int* arr, int t)
{
this->num = t;
root = new node;
root->data = arr[0];
for (int i = 1; i < num; i++)
{
insert(arr[i]);
}
}
void insert(int key)
{
node* temp = root;
node* prev = nullptr;
while (temp)
{
if (key > temp->data)
{
prev = temp;
temp = temp->right;
}
else if (key < temp->data)
{
prev = temp;
temp = temp->left;
}
}
if (key < prev->data)
{
node* tmp = new node;
tmp->data = key;
prev->left = tmp;
}
else
{
node* tmp = new node;
tmp->data = key;
prev->right = tmp;
}
}
void show(node* temp)
{
if (!temp)
{
return;
}
else
{
show(temp->left);
cout << temp->data << " ";
show(temp->right);
}
}
bool search(int key)
{
node* temp = root;
while (key != temp->data)
{
if (key < temp->data)
{
temp = temp->left;
}
else {
temp = temp->right;
}
if (temp == nullptr)
{
return false;
}
}
return true;
}
int findfather(int key1, int key2)
{
int b = max(key1, key2);
int s = min(key1, key2);
node* temp = root;
if (search(key1) == false && search(key2) == false)
{
cout << "ERROR: " << key1 << " and " << key2 << " are not found.";
return -1;
}
else if (search(key1) == false)
{
cout << "ERROR: " << key1 << " is not found.";
return -1;
}
else if (search(key2) == false)
{
cout << "ERROR: " << key2 << " is not found." ;
return -1;
}
else
{
while (!(temp->data<b && temp->data>s))
{
if (b > temp->data && s > temp->data)
{
temp = temp->right;
}
else if (b < temp->data && s < temp->data)
{
temp = temp->left;
}
else if (b == temp->data)
{
return b;
}
else if (s == temp->data)
{
return s;
}
}
return temp->data;
}
}
};
int main()
{
int n, m;
cin >> n >> m;
int* arr = new int[m];
for (int i = 0; i < m; i++)
{
cin >> arr[i];
}
tree T(arr, m);
for (int i = 0; i < n; i++)
{
int a, b;
cin >> a >> b;
int index = T.findfather(a, b);
if (index == -1)
{
}
else if (index == a)
{
cout << a << " is an ancestor of " << b << ".";
}
else if (index == b)
{
cout << b << " is an ancestor of " << a << ".";
}
else
{
cout << "LCA of " << a << " and " << b << " is " << index << ".";
}
if (i != n - 1)
{
cout << endl;
}
}
return 0;
}