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. 装服务器,测试数据库,简单的maven命令

    [说明]今天总体回顾一下:大概是早上装服务器,下午测试数据库,晚上了解简单的maven命令 一:今日完成 1)在远程服务器的tomcat 设置好管理员的登录账号 2)登录tomcat 的项目管理 查看 ...

  2. easyUI参数传递Long型时,前台解析出错的问题——SKY

    果发现datagrid在显示Long类型数据时有问题.问题如下:比如一个数据ID为20121229101239002,经过转换之后的JSON数据也没有问题,但是在显示的时候就会显示为201212291 ...

  3. lua调试小技巧

    lua中,如果碰到某个属性值改变了,但是修改的地方又特别多,调试就特别麻烦了,有个小技巧,直接贴代码 local m = {    __index = function( t, k )         ...

  4. make编译一

    在C和C++中,首先要把源文件编译成中间代码文件,在windows下就是obj文件,linux下就是.o文件:object file.这个动作叫做编译,然后再把大量的object file合成执行文件 ...

  5. Maven项目启动报错

    错误信息如下: 六月 , :: 下午 org.apache.tomcat.util.digester.SetPropertiesRule begin 警告: [SetPropertiesRule]{S ...

  6. 自编码器(autoencoder)

    今天我们会来聊聊用神经网络如何进行非监督形式的学习. 也就是 autoencoder, 自编码. 压缩与解压 有一个神经网络, 它在做的事情是 接收一张图片, 然后 给它打码, 最后 再从打码后的图片 ...

  7. web audio living

    总结网页音频直播的方案和遇到的问题. 代码:(github,待整理) 结果: 使用opus音频编码,web audio api 播放,可以达到100ms以内延时,高质量,低流量的音频直播. 背景: V ...

  8. 制作透明的图标ICO

    1.使用crowldraw画图保存为PNG格式,选择"被遮盖区域",然后保存(保存为PNG的透明格式). 2.使用IconWorkshop把透明的PNG格式导出为ICO.

  9. redis于spring整合之RedisTemplate

    原文地址: http://www.jianshu.com/p/7bf5dc61ca06

  10. python 3 json 序列化

    python 3 json 序列化 我们学习过用eval内置方法可以将一个字符串转成python对象,不过,eval方法是有局限性的,对于普通的数据类型,json.loads和eval都能用,但遇到特 ...