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 Xis 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.
 
 1 #include <stdio.h>
 2 #include <algorithm>
 3 #include <iostream>
 4 #include <map>
 5 #include <vector>
 6 #include <set>
 7 using namespace std;
 8 const int maxn=10010;
 9 int n,m,k;
10 int inorder[maxn],preorder[maxn];
11 struct node{
12     int data;
13     node* left;
14     node* right;
15 };
16 node* create(int prel,int prer,int inl,int inr){
17     if(prel>prer){
18         return NULL;
19     }
20     node* root=new node;
21     root->data=preorder[prel];
22     int k;
23     for(k=inl;k<=inr;k++){
24         if(inorder[k]==preorder[prel]){
25             break;
26         }
27     }
28     int numleft=k-inl;
29     root->left = create(prel+1,prel+numleft,inl,k-1);
30     root->right = create(prel+numleft+1,prer,k+1,inr);
31     return root;
32 }
33 bool findnode(int x){
34     for(int i=0;i<m;i++){
35         if(preorder[i]==x) return true;
36     }
37     return false;
38 }
39 node* lca(node* root,int x1,int x2){
40     if(root==NULL)return NULL;
41     if(root->data==x1 || root->data==x2) return root;
42     node* left = lca(root->left,x1,x2);
43     node* right = lca(root->right,x1,x2);
44     if(left && right) return root;
45     else if(left==NULL) return right;
46     else return left;
47 }
48 int main(){
49     scanf("%d %d",&n,&m);
50     for(int i=0;i<m;i++){
51         int c1;
52         scanf("%d",&c1);
53         inorder[i]=c1;
54     }
55     for(int i=0;i<m;i++){
56         int c1;
57         scanf("%d",&c1);
58         preorder[i]=c1;
59     }
60     node *root=create(0,m-1,0,m-1);
61     for(int i=0;i<n;i++){
62         int x1,x2;
63         scanf("%d %d",&x1,&x2);
64         if(!findnode(x1) && !findnode(x2)){
65             printf("ERROR: %d and %d are not found.\n",x1,x2);
66         }
67         else if(!findnode(x1)) printf("ERROR: %d is not found.\n",x1);
68         else if(!findnode(x2)) printf("ERROR: %d is not found.\n",x2);
69         else{
70             node* res = lca(root,x1,x2);
71             if(res->data == x1) printf("%d is an ancestor of %d.\n",x1,x2);
72             else if(res->data == x2) printf("%d is an ancestor of %d.\n",x2,x1);
73             else printf("LCA of %d and %d is %d.\n",x1,x2,res->data);
74         }
75     }
76 }
View Code

注意点:lca居然可以迭代做出来,实在是厉害,看了好久终于看懂了一点吧,考前要再背一下。一开始想的是把指定点的祖先都获得存在一个vector里,然后从后往前比较两个vector,第一个相同的就是他们的lca,但调试了半天一直报错,vector的index超出范围,不知道为什么,总感觉是对的,代码贴在下面,有大佬看到还请指点一下。

第二点的话就是二叉树的建立一定要熟悉,虽然这道题可以不用建树就做出来。

 1 #include <stdio.h>
 2 #include <algorithm>
 3 #include <iostream>
 4 #include <map>
 5 #include <vector>
 6 #include <set>
 7 using namespace std;
 8 const int maxn = 1010;
 9 int n, m, k, q = 0;
10 int inorder[maxn], preorder[maxn];
11 vector<int> v, v1[2];
12 struct node {
13     int data;
14     node* left;
15     node* right;
16 };
17 node* create(int prel, int prer, int inl, int inr) {
18     if (prel > prer) {
19         return NULL;
20     }
21     node* root = new node;
22     root->data = preorder[prel];
23     int k;
24     for (k = inl; k <= inr; k++) {
25         if (inorder[k] == preorder[prel]) {
26             break;
27         }
28     }
29     int numleft = k - inl;
30     root->left = create(prel + 1, prel + numleft, inl, k - 1);
31     root->right = create(prel + numleft + 1, prer, k + 1, inr);
32     return root;
33 }
34 void search(node* root, int x) {
35     if (root == NULL) return;
36     v.push_back(root->data);
37     if (root->data == x) {
38         for (int i = 0; i < v.size(); i++) {
39             v1[q].push_back(v[i]);
40         }
41         v.pop_back();
42         return;
43     }
44     search(root->left, x);
45     search(root->right, x);
46     v.pop_back();
47 }
48 int main() {
49     scanf("%d %d", &n, &m);
50     for (int i = 0; i < m; i++) {
51         int c1;
52         scanf("%d", &c1);
53         inorder[i] = c1;
54     }
55     for (int i = 0; i < m; i++) {
56         int c1;
57         scanf("%d", &c1);
58         preorder[i] = c1;
59     }
60     node *root = new node;
61     root = create(0, m - 1, 0, m - 1);
62     for (int i = 0; i < n; i++) {
63         int x1, x2;
64         scanf("%d %d", &x1, &x2);
65         v1[0].clear();
66         v1[1].clear();
67         v.clear();
68         q = 0;
69         search(root, x1);
70         q = 1;
71         v.clear();
72         search(root, x2);
73         if (v1[0].empty() && v1[1].empty()) {
74             printf("ERROR: %d and %d are not found.\n", x1, x2);
75         }
76         else if (v1[0].empty()) printf("ERROR: %d is not found.\n", x1);
77         else if (v1[1].empty()) printf("ERROR: %d is not found.\n", x2);
78         else {
79             for (int j = v1[0].size() - 1; j >= 0; j++) {
80                 for (int k = v1[1].size() - 1; k >= 0; k++) {
81                     if (v1[0][j] == v1[1][k]) {
82                         if (j == v1[0].size() - 1) printf("%d is an ancestor of %d.\n", x1, x2);
83                         else if (k == v1[1].size() - 1) printf("%d is an ancestor of %d.\n", x2, x1);
84                         else printf("LCA of %d and %d is %d.\n", x1, x2, v1[0][j]);
85                     }
86                 }
87             }
88         }
89     }
90 }
View Code