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 ...
随机推荐
- OKR与KPI管理的区别与联系
OKR是一种新兴的管理体系,最近几年被引进中国.由于在IT.互联网.金融.游戏等知识密集型企业中有着显著的效果,得到中国企业的认可. OKR是英文Objectives & Key Result ...
- 【BZOJ4269】再见Xor 高斯消元
[BZOJ4269]再见Xor Description 给定N个数,你可以在这些数中任意选一些数出来,每个数可以选任意多次,试求出你能选出的数的异或和的最大值和严格次大值. Input 第一行一个正整 ...
- Mac标识物理位置算法 import Levenshtein mac列表特征值
mac 字符串 与 基准字符串的 Levenshtein 距离,考虑 mac信号强度的时序性,60秒内若干次变化 不引入强度 mac字符串的唯一性 如何排序 基准字符串的选取 同一尺度 都按强度 ...
- html5 (新一代的html)
简介 h5的新特性: cavas / video / audio / cache / element / form 最小的h5文档: <!DOCTYPE html> <html> ...
- less (css预处理)
用法 1. 必须在head内 2. 样式文件必须先加载 <head> <meta charset='utf-8'> <link rel="stylesheet/ ...
- WndProc漏写override会发生什么情况?
试图改写TForm1(注意,不是TForm类)的WndProc函数,从而达到某些目的.程序如下: unit Unit1; interface uses Windows, Messages, SysUt ...
- thymeleaf基本应用
Thymeleaf是个XML/XHTML/HTML5模板引擎,可以用于Web与非Web应用. Thymeleaf的主要目标在于提供一种可被浏览器正确显示的.格式良好的模板创建方式,因此也可以用作静态建 ...
- CVPR 2018:diractNets网络,有残差网络好吗?
我把我明天讲PPT的材料弄上来了........哈 哈哈
- python基础14 ---函数模块5(模块和包)
模块与包 一.模块 1.模块是怎么诞生的. 在计算机程序的开发过程中,随着程序代码越写越多,在一个文件里代码就会越来越长,越来越不容易维护.为了编写可维护的代码,我们把很多函数分组,分别放到 不同的文 ...
- Visual Studio 2017 扩展推荐
ReSharper : 首先的是Resharper,这个基本是目前是我开发过程中必备的工具集,唯一的缺点就是吃内存,所以你的内存要是低于8G,就不要使用它了.它的特点可以快速重构.高亮显示错误.导航和 ...