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 (<= 1000), the number of pairs of nodes to be tested; and N (<= 10000), 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. 建树有两种方法,一种是把前序遍历从小到大排序就是中序遍历,然后根据前中序遍历建树。注释部分。
另一种是直接用前序遍历建树。其实中序遍历就是助于判断左右子树,用前序遍历就可以单独判断左右子树的。
代码:
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <map>
using namespace std;
struct tree
{
int Data,Height;
tree *Last,*Left,*Right;
}*head;
int q[],z[],m,n;
map<int,tree *> mp;
tree *createNode(int d,int h)
{
tree *p = new tree();
p -> Data = d;
mp[d] = p;
p -> Height = h;
p -> Last = p -> Left = p -> Right = NULL;
return p;
}
tree *createTree(int ql,int qr,int zl,int zr,int h)
{
tree *p = createNode(q[ql],h);
for(int i = zl;i <= zr;i ++)
{
if(z[i] == q[ql])
{
if(i > zl)p -> Left = createTree(ql + ,ql + i - zl,zl,i - ,h + ),p -> Left -> Last = p;
if(i < zr)p -> Right = createTree(ql + i - zl + ,qr,i + ,zr,h + ),p -> Right -> Last = p;
break;
}
}
return p;
}
tree *createTre(int l,int r,int h)
{
tree *p = createNode(q[l],h);
for(int i = l + ;i <= r + ;i ++)
{
if(i == r + || q[i] >= q[l])
{
if(i > l + )p -> Left = createTre(l + ,i - ,h + ),p -> Left -> Last = p;
if(r >= i)p -> Right = createTre(i,r,h + ),p -> Right -> Last = p;
return p;
}
}
}
void check(int a,int b)
{
if(mp[a] == NULL && mp[b] == NULL)printf("ERROR: %d and %d are not found.\n",a,b);
else if(mp[a] == NULL)printf("ERROR: %d is not found.\n",a);
else if(mp[b] == NULL)printf("ERROR: %d is not found.\n",b);
else
{
tree *t1 = mp[a],*t2 = mp[b];
while(t1 -> Height != t2 -> Height)
{
if(t1 -> Height > t2 -> Height)t1 = t1 -> Last;
else t2 = t2 -> Last;
}
if(t1 == t2)
{
printf("%d is an ancestor of %d.\n",t1 -> Data,a == t1 -> Data ? b : a);
return;
}
t1 = t1 -> Last;
t2 = t2 -> Last;
while(t1 != t2)
{
t1 = t1 -> Last;
t2 = t2 -> Last;
}
printf("LCA of %d and %d is %d.\n",a,b,t1 -> Data);
}
}
int main()
{
int a,b;
scanf("%d%d",&m,&n);
for(int i = ;i < n;i ++)
{
scanf("%d",&q[i]);
//z[i] = q[i];
}
//sort(z,z + n);
// head = createTree(0,n - 1,0,n - 1,0);
head = createTre(,n - ,);
for(int i = ;i < m;i ++)
{
scanf("%d%d",&a,&b);
check(a,b);
}
}

1143. Lowest Common Ancestor (30)的更多相关文章

  1. PAT Advanced 1143 Lowest Common Ancestor (30) [二叉查找树 LCA]

    题目 The lowest common ancestor (LCA) of two nodes U and V in a tree is the deepest node that has both ...

  2. [PAT] 1143 Lowest Common Ancestor(30 分)

    1143 Lowest Common Ancestor(30 分)The lowest common ancestor (LCA) of two nodes U and V in a tree is ...

  3. PAT 1143 Lowest Common Ancestor[难][BST性质]

    1143 Lowest Common Ancestor(30 分) The lowest common ancestor (LCA) of two nodes U and V in a tree is ...

  4. 1143 Lowest Common Ancestor

    The lowest common ancestor (LCA) of two nodes U and V in a tree is the deepest node that has both U ...

  5. PAT 甲级 1143 Lowest Common Ancestor

    https://pintia.cn/problem-sets/994805342720868352/problems/994805343727501312 The lowest common ance ...

  6. PAT 1143 Lowest Common Ancestor

    The lowest common ancestor (LCA) of two nodes U and V in a tree is the deepest node that has both U ...

  7. [PAT] 1143 Lowest Common Ancestor(30 分)1145 Hashing - Average Search Time(25 分)

    1145 Hashing - Average Search Time(25 分)The task of this problem is simple: insert a sequence of dis ...

  8. PAT甲级1143 Lowest Common Ancestor【BST】

    题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805343727501312 题意: 给定一个二叉搜索树,以及他的前 ...

  9. PAT_A1143#Lowest Common Ancestor

    Source: PAT A1143 Lowest Common Ancestor (30 分) Description: The lowest common ancestor (LCA) of two ...

随机推荐

  1. iOS tableView自定义删除按钮

    // 自定义左滑显示编辑按钮 - (NSArray<UITableViewRowAction*>*)tableView:(UITableView *)tableView editActio ...

  2. 像使用linux一样使用mac

    1 不能像使用windows一样使用mac 因为mac卸载不方便. 2 gcc的问题 就使用系统默认的gcc,即clang,要想使用原声的gcc是不行的,mac本身不支持.

  3. 我的Android进阶之旅------>Android字符串资源中的单引号问题error: Apostrophe not preceded by 的解决办法

    刚刚在string字符串资源文件中,写了一个单引号,报错了,错误代码如下 error: Apostrophe not preceded by \ (in OuyangPeng's blog ) 资源文 ...

  4. 我的Android进阶之旅------>如何将Android源码导入Eclipse中来查看(非常实用)

    Android源码下载完成的目录结构如如所示: step1:将.classpath文件拷贝到源代码的根目录 Android源码支持多种IDE,如果是针对APP层做开发的话,建议大家使用Eclipse开 ...

  5. Maven下载、安装和配置(转发:http://blog.csdn.net/jiuqiyuliang/article/details/45390313)

    准备工作 java开发环境(JDK) maven下载地址:http://maven.apache.org/release-notes-all.html 安装 安装maven超级简单,总共分四步: 下载 ...

  6. 【logback】认识logback

    一. Reference:http://www.cnblogs.com/yongze103/archive/2012/05/05/2484753.html 1. Logback为取代log4j而生,l ...

  7. 用cocos2d-html5做的消除类游戏《英雄爱消除》(4)——游戏结束

    游戏结束界面: 在前面几个教程中,这个界面的创作所需要的知识点基本我们都讲过了,这里就说下用户数据的缓存吧,也是先来看下源码 /** * Power by html5中文网(html5china.co ...

  8. 苹果企业账号发布APP详解——通过自己网站分发应用

    一.通过企业账号申请证书 1 Certificate Signing Request (CSR)文件 在Mac系统中进入“钥匙串访问”,选择“钥匙串访问”-“证书助理”-“从证书颁发机构请求证书…”, ...

  9. Python 3 并发编程多进程之队列(推荐使用)

    Python 3 并发编程多进程之队列(推荐使用) 进程彼此之间互相隔离,要实现进程间通信(IPC),multiprocessing模块支持两种形式:队列和管道,这两种方式都是使用消息传递的. 可以往 ...

  10. python正则-- re模块

    匹配数字相关'.' 默认匹配除\n之外的任意一个字符,若指定flag DOTALL,则匹配任意字符,包括换行'^' 匹配字符开头,若指定flags MULTILINE,这种也可以匹配上(r" ...