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.
note:
1.根据前序中序建树
2.键值 结点指针 构造map 高效搜寻结点
3.若待查询节点存在,用 LCA( )查找最近公共祖先 LCA有三种 u,v都在左子树或右子树,或者u,v分别在左子树和右子树
LCA算法:
(1)当前遍历的节点是否为空,返回null
(2)当前节点数据=u|v?是的话返回当前节点(找到了)
(3)然后判断left 和right是否为空, 若都不为空 (即分居左右),则当前root为祖先指针, 若其中一个为空,说明查找的两个值其中有一个在一边先找到了,此时返回另一边,得到值为u或v的结点,返回该结点,当树中的确存在u,v的结点时,这个情况下出现 u(v)是v(u)的祖先 看谁先找到返回
#include <iostream>
#include <map>
using namespace std;
struct node
{
int data;
struct node *lchild, *rchild;
};
map<int,node*>mp;
const int max_size(10001);
int ino[max_size],pre[max_size];
node*buildtree(int preL,int preR,int inoL,int inoR)
{
if(preL>preR)return NULL;
int e=pre[preL],idx=inoL;
while(e!=ino[idx]&&idx<=inoR)idx++;
node *root=new node;
root->data=e;
root->lchild=root->rchild=NULL;
int leftLength=idx-inoL;
int leftPreEnd=leftLength+preL;
root->lchild=buildtree(preL+1, leftPreEnd, inoL, idx-1);
root->rchild=buildtree(leftPreEnd+1,preR,idx+1,inoR);
mp[e]=root;
return root;
}
node*LCA(node*root,int u,int v)
{
if(root==NULL)return NULL;
if(root->data==u||root->data==v)return root;
node*left=LCA(root->lchild,u,v);
node*right=LCA(root->rchild,u,v);
if(left&&right)return root;
return left==NULL?right:left;
}
int main()
{
int m,n;
scanf("%d %d",&m,&n);
for(int i=0;i<n;i++)scanf("%d",&ino[i]);
for(int i=0;i<n;i++)scanf("%d",&pre[i]);
node*root=buildtree(0,n-1,0,n-1);
while(m--)
{
int u,v;
scanf("%d %d",&u,&v);
node*sx=mp.count(u)?mp[u]:NULL;
node*sy=mp.count(v)?mp[v]:NULL;
if(!sx&&!sy)printf("ERROR: %d and %d are not found.\n",u,v);
else if(!sx||!sy)printf("ERROR: %d is not found.\n",sx?v:u);
else
{
node*lca=LCA(root,u,v);
if(lca!=sx&&lca!=sy)printf("LCA of %d and %d is %d.\n",u,v,lca->data);
else printf("%d is an ancestor of %d.\n",lca==sx?u:v,lca==sx?v:u);
}
}
}