Source:
Description:
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 andA
is the key. But ifA
is one of U and V, printX is an ancestor of Y.
whereX
isA
andY
is the other node. If U or V is not found in the binary tree, print in a lineERROR: U is not found.
orERROR: V is not found.
orERROR: 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.
Keys:
- 二叉树的建立
- 二叉树的遍历
- LCA算法
Attention:
- 没有卡时间,所以常规操作就能通过,基本功要扎实;
- 键值能取到int的最大值,数组开不了那么大的hash,只能用map了;
Code:
1 /* 2 Data: 2019-05-13 21:06:50 3 Problem: PAT_A1151#LCA in a Binary Tree 4 AC: 57:30 5 6 题目大意: 7 求U和V最近公共祖先 8 输入: 9 第一行给出,M测试组数<=1e3,N结点数<=1e4 10 接下来两行给出,中序遍历和先序遍历 11 接下来M行给出,给出查询结点U和V 12 输出: 13 如果U和V有公共祖先,LCA of U and V is A. 其中,A是key值 14 如果U是V的祖先,A is an ancestor of V. 其中,A是key值,V是结点序号 15 如果U或V未找到,ERROR: U is not found. 16 如果U和V均未找到,ERROR: U and V are not found. 17 */ 18 19 #include<cstdio> 20 #include<stack> 21 #include<map> 22 using namespace std; 23 const int M=1e4+10; 24 int in[M],pre[M],v1,v2,f1,f2; 25 stack<int> p1,p2; 26 map<int,int> key; 27 struct node 28 { 29 int data; 30 node *lchild,*rchild; 31 }; 32 33 node* Create(int preL,int preR,int inL,int inR) 34 { 35 if(preL > preR) 36 return NULL; 37 node *root = new node; 38 root->data = pre[preL]; 39 int k; 40 for(k=inL; k<=inR; k++) 41 if(in[k]==root->data) 42 break; 43 int numLeft = k-inL; 44 root->lchild = Create(preL+1,preL+numLeft,inL,k-1); 45 root->rchild = Create(preL+numLeft+1,preR,k+1,inR); 46 return root; 47 } 48 49 void DFS(node *root) 50 { 51 if(!root) 52 return; 53 if(f1) p1.push(root->data); 54 if(f2) p2.push(root->data); 55 if(root->data==v1) f1=0; 56 if(root->data==v2) f2=0; 57 DFS(root->lchild); 58 DFS(root->rchild); 59 if(f1) p1.pop(); 60 if(f2) p2.pop(); 61 } 62 63 int main() 64 { 65 #ifdef ONLINE_JUDGE 66 #else 67 freopen("Test.txt", "r", stdin); 68 #endif 69 70 int n,m; 71 scanf("%d%d", &m,&n); 72 for(int i=0; i<n; i++) 73 scanf("%d", &in[i]); 74 for(int i=0; i<n; i++) 75 { 76 scanf("%d", &pre[i]); 77 key[pre[i]]=1; 78 } 79 node *root = Create(0,n-1,0,n-1); 80 for(int i=0; i<m; i++) 81 { 82 scanf("%d%d",&v1,&v2); 83 if(key[v1]==0 && key[v2]==0) 84 printf("ERROR: %d and %d are not found.\n", v1,v2); 85 else if(key[v1]==0 || key[v2]==0) 86 printf("ERROR: %d is not found.\n", key[v2]==0?v2:v1); 87 else 88 { 89 while(!p1.empty()) p1.pop(); 90 while(!p2.empty()) p2.pop(); 91 f1=1;f2=1; 92 DFS(root); 93 while(p1.size() > p2.size()) p1.pop(); 94 while(p2.size() > p1.size()) p2.pop(); 95 while(p1.top() != p2.top()) 96 {p1.pop();p2.pop();} 97 if(p1.top()!=v1 && p1.top()!=v2) 98 printf("LCA of %d and %d is %d.\n",v1,v2,p1.top()); 99 else if(p1.top() == v1) 100 printf("%d is an ancestor of %d.\n", v1,v2); 101 else 102 printf("%d is an ancestor of %d.\n", v2,v1); 103 } 104 } 105 106 return 0; 107 }