题目
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:
6 8
7 2 3 4 6 5 1 8
5 3 7 2 6 4 8 1
2 6
8 1
7 9
12 -3
0 8
99 99
Sample Output:
LCA of 2 and 6 is 3.
8 is an ancestor of 1.
ERROR: 9 is not found.
ERROR: 12 and -3 are not found.
ERROR: 0 is not found.
ERROR: 99 and 99 are not found.
思路
重建树 + LCA
代码
版本1
#include <iostream>
using namespace std;
const int MAXN = 10005;
int in[MAXN];
int pre[MAXN];
struct node{
int data;
node* left;
node* right;
// 初始化列表形式的构造函数
//node(int key):data(key){}
};
node* rebuildTree(int inL, int inR, int preL, int preR){
if(inL > inR || preL > preR) return NULL;
int dataOfRoot = pre[preL];
int len = 0;
int k;
for(k=inL; k<=inR; k++, len++){
if(in[k] == dataOfRoot) break;
}
node* root = new node();
root->data = dataOfRoot;
root->left = rebuildTree(inL, k-1, preL+1, preL+len);
root->right = rebuildTree(k+1, inR, preL+len+1, preR);
return root;
}
void inSearch(node* T){
if(T == NULL) return;
inSearch(T->left);
printf("%d ", T->data);
inSearch(T->right);
}
node* lca(int u, int v, node* T){
if(T == NULL) return NULL;
if(T->data == u || T->data == v) return T;// 其中一个和根节点相同
node* l = lca(u, v, T->left);
node* r = lca(u, v, T->right);
if(l == NULL) return r;
if(r == NULL) return l;
return T;
}
int main(){
int m, n;
int u, v;
scanf("%d %d", &m, &n);
for(int i=0; i<n; i++){
scanf("%d", &in[i]);
}
for(int i=0; i<n; i++){
scanf("%d", &pre[i]);
}
node* T = rebuildTree(0, n-1, 0, n-1);
//inSearch(T);
for(int i=0; i<m; i++){
scanf("%d %d", &u, &v);
int j;
int cnt = 0, flag;
for(j=0; j<n; j++) if(in[j] == u) break;
if(j == n){
cnt++;// 没找到
flag = 0;
}
for(j=0; j<n; j++) if(in[j] == v) break;
if(j == n){
cnt++;// 没找到
flag = 1;
}
if(cnt == 0){// 都在
// 判断祖先
int rootOfUV = lca(u, v, T)->data;
if(rootOfUV == v){
printf("%d is an ancestor of %d.\n", v, u);
}
else if(rootOfUV == u){
printf("%d is an ancestor of %d.\n", u, v);
}
else{
printf("LCA of %d and %d is %d.\n", u, v, rootOfUV);
}
}
else if(cnt == 1){
printf("ERROR: %d is not found.\n", flag ? v : u);
}
else{
printf("ERROR: %d and %d are not found.\n", u, v);
}
}
return 0;
}
版本2
参考了柳神的代码之后修改,但是仍重建树,主要修改地方在 u,v 是否在数组中的判断部分,其余未修改
#include <iostream>
#include <map>
using namespace std;
const int MAXN = 10005;
int in[MAXN];
int pre[MAXN];
map<int, int> pos;
struct node{
int data;
node* left;
node* right;
// 初始化列表形式的构造函数
//node(int key):data(key){}
};
node* rebuildTree(int inL, int inR, int preL, int preR){
if(inL > inR || preL > preR) return NULL;
int dataOfRoot = pre[preL];
int len = 0;
int k;
for(k=inL; k<=inR; k++, len++){
if(in[k] == dataOfRoot) break;
}
node* root = new node();
root->data = dataOfRoot;
root->left = rebuildTree(inL, k-1, preL+1, preL+len);
root->right = rebuildTree(k+1, inR, preL+len+1, preR);
return root;
}
node* lca(int u, int v, node* T){
if(T == NULL) return NULL;
if(T->data == u || T->data == v) return T;// 其中一个和根节点相同
node* l = lca(u, v, T->left);
node* r = lca(u, v, T->right);
if(l == NULL) return r;
if(r == NULL) return l;
return T;
}
int main(){
int m, n;
int u, v;
scanf("%d %d", &m, &n);
for(int i=0; i<n; i++){
scanf("%d", &in[i]);
pos[in[i]] = i + 1;// 如果不赋值,那么 int 的默认值为 0
// 这里我们都把初始值设为大于 0 的数,用于下面的判断
}
for(int i=0; i<n; i++){
scanf("%d", &pre[i]);
}
node* T = rebuildTree(0, n-1, 0, n-1);
//inSearch(T);
for(int i=0; i<m; i++){
scanf("%d %d", &u, &v);
if(pos[u] == 0 && pos[v] == 0){// 都不在
printf("ERROR: %d and %d are not found.\n", u, v);
}
else if(pos[u] == 0 || pos[v] == 0){
printf("ERROR: %d is not found.\n", pos[u] ? v : u);
}
else{
// 判断祖先
int rootOfUV = lca(u, v, T)->data;
if(rootOfUV == v){
printf("%d is an ancestor of %d.\n", v, u);
}
else if(rootOfUV == u){
printf("%d is an ancestor of %d.\n", u, v);
}
else{
printf("LCA of %d and %d is %d.\n", u, v, rootOfUV);
}
}
}
return 0;
}
参考文献
- [1] https://blog.youkuaiyun.com/zhaoyunfullmetal/article/details/46924629
- [2] https://blog.youkuaiyun.com/liuchuo/article/details/82560863