A1143/A1151 Lowest Common Ancestor
1143 Lowest Common Ancestor
Link
1143 Lowest Common Ancestor (30 分)
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.
A binary search tree (BST) is recursively defined as a binary tree which has the following properties:
The left subtree of a node contains only nodes with keys less than the node’s key.
The right subtree of a node contains only nodes with keys greater than or equal to the node’s key.
Both the left and right subtrees must also be binary search trees.
Given any two nodes in a BST, 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 BST, respectively. In the second line, N distinct integers are given as the preorder traversal sequence of the BST. 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 BST, 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
6 3 1 2 5 4 8 7
2 5
8 7
1 9
12 -3
0 8
99 99
Sample Output
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.
Topic
找一个二叉搜索树中两个结点的最低公共祖先。
Analysis & Code
1、Create a BST
.
node*Preorder(int h,int t)
{
if(h>t)
return nullptr;
node*p = new node;
p->data = preorder[h];
mp[p->data] = p;
int i;
for(i=h+1;i<=t;i++)
if(preorder[i]>preorder[h])
break;
p->left = Preorder(h+1,i-1);
p->right = Preorder(i,t);
return p;
}
2、找a,b两点最低公共祖先,从根一直往下找,不断更新current值,直到不满足平衡二叉树的要求。
while((a>current->data && b>current->data) || (a<current->data && b<current->data))
{
if(a>current->data && b>current->data)
current = current->right;
else
current = current->left;
}
3、达到2
的要求之后,分三种情况:①current为a,b的祖先,②a=current,③b=current
if((a>current->data && b<current->data) || (a<current->data && b>current->data))
printf("LCA of %d and %d is %d.\n",a,b,current->data);
else if(a == current->data)
printf("%d is an ancestor of %d.\n",a,b);
else
printf("%d is an ancestor of %d.\n",b,a);
Code:
#include<bits/stdc++.h>
using namespace std;
struct node
{
int data;
node*left,*right;
};
int n,m,preorder[10100];
unordered_map<int,node*> mp;
node*Preorder(int h,int t)
{
if(h>t)
return nullptr;
node*p = new node;
p->data = preorder[h];
mp[p->data] = p;
int i;
for(i=h+1;i<=t;i++)
if(preorder[i]>preorder[h])
break;
p->left = Preorder(h+1,i-1);
p->right = Preorder(i,t);
return p;
}
int main()
{
cin>>m>>n;
for(int i=0;i<n;i++)
cin>>preorder[i];
node*root = Preorder(0,n-1);
while(m--)
{
int a,b;
cin>>a>>b;
if(!mp.count(a) && !mp.count(b))
printf("ERROR: %d and %d are not found.\n",a,b);
else if(!mp.count(a))
printf("ERROR: %d is not found.\n",a);
else if(!mp.count(b))
printf("ERROR: %d is not found.\n",b);
else
{
node *current = root;
while((a>current->data && b>current->data) || (a<current->data && b<current->data))
{
if(a>current->data && b>current->data)
current = current->right;
else
current = current->left;
}
if((a>current->data && b<current->data) || (a<current->data && b>current->data))
printf("LCA of %d and %d is %d.\n",a,b,current->data);
else if(a == current->data)
printf("%d is an ancestor of %d.\n",a,b);
else
printf("%d is an ancestor of %d.\n",b,a);
}
}
}
1151 LCA in a Binary Tree
Link
1151 LCA in a Binary Tree (30 分)
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.
Topic
给出二叉树的中序、先序遍历 构建二叉树,并找一个二叉树中两个结点的最低公共祖先。
Analysis & Code
1、Create a binary tree
according to inorder and preorder traversal sequences of the binary tree.
node* Create(int prel,int prer,int inl,int inr)
{
if(prel>prer)
return nullptr;
node* root = new node;
root->data = preorder[prel];
mp[root->data] = root;
int k;
for(k=inl;k<=inr;k++)
if(inorder[k] == preorder[prel])
break;
root->stu = k;
int numleft = k - inl;
root->left = Create(prel+1,prel+numleft,inl,k-1);
root->right = Create(prel+numleft+1,prer,k+1,inr);
return root;
}
其他部分和1143题没太多区别。本题给的是二叉树,发现二叉树的中序遍历结果就是二叉搜索树,所以另建一个stu
保存新节点。
Code
#include<bits/stdc++.h>
using namespace std;
struct node
{
int data,stu;
node*left,*right;
};
int m,n;
int preorder[10010],inorder[10010];
unordered_map<int,node*> mp;
node* Create(int prel,int prer,int inl,int inr)
{
if(prel>prer)
return nullptr;
node* root = new node;
root->data = preorder[prel];
mp[root->data] = root;
int k;
for(k=inl;k<=inr;k++)
if(inorder[k] == preorder[prel])
break;
root->stu = k;
int numleft = k - inl;
root->left = Create(prel+1,prel+numleft,inl,k-1);
root->right = Create(prel+numleft+1,prer,k+1,inr);
return root;
}
int main()
{
cin>>m>>n;
for(int i=0;i<n;i++)
cin>>inorder[i];
for(int i=0;i<n;i++)
cin>>preorder[i];
node* root = Create(0,n-1,0,n-1);
while(m--)
{
int a,b,na,nb;
cin>>a>>b;
if(!mp.count(a) && !mp.count(b))
printf("ERROR: %d and %d are not found.\n",a,b);
else if(!mp.count(a))
printf("ERROR: %d is not found.\n",a);
else if(!mp.count(b))
printf("ERROR: %d is not found.\n",b);
else
{
na = mp[a]->stu;
nb = mp[b]->stu;
node* t = root;
while((na>t->stu&&nb>t->stu)||(na<t->stu&&nb<t->stu))
{
if(na>t->stu&&nb>t->stu)
t = t->right;
else
t = t->left;
}
if((na>t->stu&&nb<t->stu)||(na<t->stu&&nb>t->stu))
printf("LCA of %d and %d is %d.\n",a,b,t->data);
else if(na == t->stu)
printf("%d is an ancestor of %d.\n",a,b);
else
printf("%d is an ancestor of %d.\n",b,a);
}
}
}