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.
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.
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..
- 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
- 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.
不需要建树,只需要记录每个值的父节点是谁即可。
给定前序和中序序列,递归,map记录每个值父节点在前序序列中的下标,根节点的父节点的下标是-1,先判断所给的一对节点是否都存在,若存在,通过map来形成两个节点各自从该节点到根节点的值的序列,然后倒序遍历两个序列,当找到最后一个相同的值的时候,就是这两个节点的最低公共祖先LCA。
- #include
- #include
- #include
- using namespace std;
-
- int n, m, cnt1, cnt2, lca, u, v, nu, nv;
- int midorder[10010], preorder[10010], t1[10010], t2[10010];
- bool flag1, flag2;
- unordered_map<int, int>q;
-
- void build(int fa, int mid_l, int mid_r, int pre_l, int pre_r) {
- if (mid_l > mid_r || pre_l > pre_r) {
- return;
- }
- q[preorder[pre_l]] = fa;
- int t = mid_l;
- while (preorder[pre_l] != midorder[t]) {
- t++;
- }
- int len = t - mid_l;
- build(pre_l, mid_l, t - 1, pre_l + 1, pre_l + len);
- build(pre_l, t + 1, mid_r, pre_l + len + 1, pre_r);
- }
-
- int main() {
- scanf("%d %d", &m, &n);
- for (int i = 0; i < n; i++) {
- scanf("%d", &midorder[i]);
- }
- for (int i = 0; i < n; i++) {
- scanf("%d", &preorder[i]);
- }
- build(-1, 0, n - 1, 0, n - 1);
- unordered_map<int, int>::iterator it1, it2;
- while (m--) {
- scanf("%d %d", &u, &v);
- flag1 = flag2 = 0;
- cnt1 = cnt2 = 0;
- it1 = q.find(u);
- it2 = q.find(v);
- if (q.find(u) != q.end()) {
- flag1 = 1;
- }
- if (q.find(v) != q.end()) {
- flag2 = 1;
- }
- if (!flag1 && !flag2) {
- printf("ERROR: %d and %d are not found.\n", u, v);
- } else if (!flag1 && flag2) {
- printf("ERROR: %d is not found.\n", u);
- } else if (flag1 && !flag2) {
- printf("ERROR: %d is not found.\n", v);
- } else {
- nu = u, nv = v;
- while (true) {
- t1[cnt1++] = nu;
- if (q[nu] == -1) {
- break;
- }
- nu = preorder[q[nu]];
- }
- while (nv != -1) {
- t2[cnt2++] = nv;
- if (q[nv] == -1) {
- break;
- }
- nv = preorder[q[nv]];
- }
- for (int i = cnt1 - 1, j = cnt2 - 1; i >= 0 && j >= 0; i--, j--) {
- if (t1[i] == t2[j] && (i == 0 || j == 0 || t1[i - 1] != t2[j - 1])) {
- lca = t1[i];
- break;
- }
- }
- if (u == lca) {
- printf("%d is an ancestor of %d.\n", u, v);
- } else if (v == lca) {
- printf("%d is an ancestor of %d.\n", v, u);
- } else {
- printf("LCA of %d and %d is %d.\n", u, v, lca);
- }
- }
- }
- return 0;
- }