1143. Lowest Common Ancestor (30)
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)的更多相关文章
- 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 ...
- [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 ...
- 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 ...
- 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 ...
- PAT 甲级 1143 Lowest Common Ancestor
https://pintia.cn/problem-sets/994805342720868352/problems/994805343727501312 The lowest common ance ...
- 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 ...
- [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 ...
- PAT甲级1143 Lowest Common Ancestor【BST】
题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805343727501312 题意: 给定一个二叉搜索树,以及他的前 ...
- PAT_A1143#Lowest Common Ancestor
Source: PAT A1143 Lowest Common Ancestor (30 分) Description: The lowest common ancestor (LCA) of two ...
随机推荐
- task15-18
[说明]貌似maven在真实的项目实战中挺重要的,可以省去大量的工作,有必要单独学习一下 15.创建一个新的maven项目 16.在src/main/java下随便创建一个java文件,clean,i ...
- Jmeter 02 JMeter体系结构
1. Jmeter简介 2. Jmeter体系结构 3. Jmeter运行原理 4. Jmeter测试计划要素 5. Jmeter环境介绍 6. Jmeter与Loadrunner异同
- 【BZOJ3488】[ONTAK2010]Highways 扫描线+树状数组
[BZOJ3488][ONTAK2010]Highways Description 给一棵n个点的树以及m条额外的双向边q次询问,统计满足以下条件的u到v的路径:恰经过一条额外的边不经过树上u到v的路 ...
- HTML/CSS/JS初始化
CSS <link type="text/css" href="http://www.mazey.cn/css/mazey-base.css" rel=& ...
- Android笔记之WebView加载网页的进度回调
wv.setWebChromeClient(new WebChromeClient(){ @Override public void onProgressChanged(WebView view, i ...
- 类加载(一):static块 和 Class.forName
1. class Some { static{ System.out.println("1"); } public Some(){ System.out.println(" ...
- Android动画效果animation
1.Tween 根据指定动画开始和结束时的对象属性(位置.Alpha值(透明度).大小.角度等)以及动画播放的时间长度生成动画: 2.Frame 指定每一帧所播放的图片和时间长度. 建立动画的方法 ...
- redis学习笔记 - Pipeline与事务
原文 Redis提供了5种数据结构,但除此之外,Redis还提供了注入慢查询分析,Redis Shell.Pipeline.事务.与Lua脚本.Bitmaps.HyperLogLog.PubSub.G ...
- 每天一个Linux命令(27)gzip命令
zip命令用来压缩文件.gzip是个使用广泛的压缩程序,文件经它压缩过后,其名称后面会多处“.gz”扩展名. (1)用法: 用法: gzip [选项参数][-s <压缩字尾字符 ...
- 声明:关于该博客部分Java等方向知识参考来源的说明
[声明] 该博客部分代码是通过学习黑马程序员(传智播客)视频后,参考毕向东.张孝祥.杨中科等老师的公开课视频中讲解的代码,再结合自己的理解,自己手敲上去的,一方面加深自己的理解和方便以后自己用到的时候 ...