题目https://pintia.cn/problem-sets/994805342720868352/problems/994805343727501312

题意:

给定一个二叉搜索树,以及他的前序遍历序列。

现在有m组询问,对于给定的两个关键字值u,v问他们的最近公共祖先是谁。

思路:

根本跟LCA都没关系好吗。m不是很大,每组询问都查找一次u和v就行了。

因为是BST所以根据前序序列可以直接建出这棵树。

然后在树上查找u和v。

本来还用了一个vector分别存根到u,v的路径,然后两个循环暴力找到最近公共祖先。然后超时了。

其实根本不用这么麻烦,只用在查找的过程中用一个vis数组来标记上一次查找是否已经经过这个点了。如果已经访问过,那么说明这个祖先是他们公共的,更新答案。

因为是从顶开始的遍历所以最后结束时的答案肯定是最近的公共祖先。

 //#include<bits/stdc++>
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<stdlib.h>
#include<queue>
#include<map>
#include<stack>
#include<set> #define LL long long
#define ull unsigned long long
#define inf 0x3f3f3f3f using namespace std; int n, m;
const int maxn = 1e4 + ;
int preorder[maxn]; struct node{
int lchild = -, rchild = -;
int key;
}tree[maxn];
int tot = ; bool Insert(int val, int rt)
{
if(val < tree[rt].key){
if(tree[rt].lchild == -){
tree[tot].key = val;
tree[rt].lchild = tot++;
return true;
}
else return Insert(val, tree[rt].lchild);
}
else if(val > tree[rt].key){
if(tree[rt].rchild == -){
tree[tot].key = val;
tree[rt].rchild = tot++;
return true;
}
else return Insert(val, tree[rt].rchild);
}
else return true;
} //vector<int>path[2];
int vis[maxn], ans;
bool ffind(int val)
{
// path[u].clear();
//memset(vis, 0, sizeof(vis));
int now = ;
while(now != -){
if(tree[now].key == val){
if(vis[now])ans = now;
else vis[now] = true;
// path[u].push_back(now);
return true;
}
else if(val < tree[now].key){
if(vis[now])ans = now;
else vis[now] = true;
// path[u].push_back(now);
now = tree[now].lchild;
}
else if(val > tree[now].key){
if(vis[now])ans = now;
else vis[now] = true;
// path[u].push_back(now);
now = tree[now].rchild;
}
}
if(now == -)return false;
} void printtree(int rt)
{
if(rt == -)return;
printf("%d ", tree[rt].key);
printtree(tree[rt].lchild);
printtree(tree[rt].rchild);
return;
} int main()
{
scanf("%d%d", &m, &n);
tree[].key = inf;
for(int i = ; i < n; i++){
scanf("%d", &preorder[i]);
Insert(preorder[i], );
}
//printtree(1);
while(m--){
int u, v;
scanf("%d%d", &u, &v);
ans = -;
memset(vis, , sizeof(vis));
bool fu = ffind(u);
bool fv = ffind(v);
if(!fu && !fv){
printf("ERROR: %d and %d are not found.\n", u, v);
}
else if(!fu){
printf("ERROR: %d is not found.\n", u);
}
else if(!fv){
printf("ERROR: %d is not found.\n", v);
}
else{
// for(int i = 0; i < path[0].size(); i++)printf("%d ", tree[path[0][i]].key);printf("\n");
// for(int j = 0; j < path[1].size(); j++)printf("%d ", tree[path[1][j]].key);printf("\n"); if(tree[ans].key == u){
printf("%d is an ancestor of %d.\n", u, v);
}
else if(tree[ans].key == v){
printf("%d is an ancestor of %d.\n", v, u);
}
else if(ans != -){
printf("LCA of %d and %d is %d.\n", u, v, tree[ans].key);
}
}
} return ;
}

PAT甲级1143 Lowest Common Ancestor【BST】的更多相关文章

  1. PAT 甲级 1143 Lowest Common Ancestor

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

  2. 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 ...

  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. [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 ...

  5. 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 ...

  6. 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. 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 ...

  8. [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 ...

  9. Lowest Common Ancestor of a Binary Search Tree (BST)

    Given a binary search tree(BST), find the lowest common ancestor of two given nodes in the BST. Node ...

随机推荐

  1. ionic使用iframe时无法显示网页或报错

    ionic使用iframe时无法显示网页或报错 Uncaught DOMException: Blocked a frame with origin 在config.xml中添加 <access ...

  2. Docker Mysql数据库主从同步配置方法

    一.背景 最近在做内部平台架构上的部署调整,顺便玩了一下数据库的主从同步,特此记录一下操作- 二.具体操作 1.先建立数据存放目录(-/test/mysql_test/) --mysql --mast ...

  3. java 定时任务之一 @Scheduled注解(第一种方法)

    使用spring @Scheduled注解执行定时任务: 运行!!! 关于Cron表达式(转载) 表达式网站生成: http://cron.qqe2.com/  直接点击 作者:http://blog ...

  4. 谈谈tmpdir与innodb_tmpdir的区别和用处

    [背景] innodb_tmpdir是在innodb online ddl中提到的一个参数:大致的意思是innodb在做online-ddl的时候会向临时目录写入“临时排序文件” 而这些文件的大小基本 ...

  5. EPOLL AND Nonblocking I/O

    https://medium.com/@copyconstruct/nonblocking-i-o-99948ad7c957 https://idndx.com/2014/09/02/the-impl ...

  6. idea 下 encodings.xml 的正确位置

    在多个module存在的情况下 encodings.xml在 project 下的.idea 下面         这个就是最父级project

  7. KM算法小结

    最近有一个需求,主要内容如下: APP一般刷新一次,会返回6个Item(6可能会变),每个Item都要展示一个广告,其中每个Item会发送一个请求,返回的结果是一个广告数组,比如[ad1, ad2, ...

  8. SQLServer截取字符串常用函数

    SQL Server中一共提供了三个字符串截取函数:LEFT().RIGHT().SUBSTRING(). 一.LEFT()函数 函数说明如下: 语法:LEFT(character,integer). ...

  9. JDK 自带的观察者模式源码分析以及和自定义实现的取舍

    前言 总的结论就是:不推荐使用JDK自带的观察者API,而是自定义实现,但是可以借鉴其好的思想. java.util.Observer 接口源码分析 该接口十分简单,是各个观察者需要实现的接口 pac ...

  10. Linux之文件系统各种符号说明

    / 根目录 唯一必须挂载的目录.不要有任何的犹豫,选一个分区,挂载它!(在绝大多数情况下,有10G的容量应该是够用了.当然了,很多东西都是多多益善的) /boot 它包含了操作系统的内核和在启动系统过 ...