PAT甲级1143 Lowest Common Ancestor【BST】
题目: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】的更多相关文章
- PAT 甲级 1143 Lowest Common Ancestor
https://pintia.cn/problem-sets/994805342720868352/problems/994805343727501312 The lowest common ance ...
- 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[难][BST性质]
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(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
The lowest common ancestor (LCA) of two nodes U and V in a tree is the deepest node that has both U ...
- 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 ...
- 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 ...
- [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 ...
- 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 ...
随机推荐
- Linux 端口转发一则
目前已知的,公司的网络只有 80.8008(后来又给关了 - -).443 端口的 TCP 可以出去,其它已知的所有端口都被封锁.所以,我的***工具就歇菜了. 最后尝试,通过配置端口转发,将ss主机 ...
- C#面试题(转载) SQL Server 数据库基础笔记分享(下) SQL Server 数据库基础笔记分享(上) Asp.Net MVC4中的全局过滤器 C#语法——泛型的多种应用
C#面试题(转载) 原文地址:100道C#面试题(.net开发人员必备) https://blog.csdn.net/u013519551/article/details/51220841 1. . ...
- Linux系统下x86和ARM的区别有哪些?
问题: 最近在用三星的一款i5处理器的Windows平板,和iPad,以及其他使用ARM处理器的手机相比,发热量大很多,甚至需要借助风扇来散热,耗电量也大了不少. 那么就很奇怪,在主频相差不大,并且实 ...
- Effective Java 第三版——66. 明智谨慎地使用本地方法
Tips 书中的源代码地址:https://github.com/jbloch/effective-java-3e-source-code 注意,书中的有些代码里方法是基于Java 9 API中的,所 ...
- Netty学习(八)-Netty的心跳机制
版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/a953713428/article/details/69378412我们知道在TCP长连接或者Web ...
- Mybatis(三) 映射文件详解
前面说了全局配置文件中内容的详解,大家应该清楚了,现在来说说这映射文件,这章就对输入映射.输出映射.动态sql这几个知识点进行说明,其中高级映射(一对一,一对多,多对多映射)在下一章进行说明. 一.输 ...
- Troubleshooting Scheduler Autotask Issues (Doc ID 1561498.1)
In this Document Purpose Troubleshooting Steps References APPLIES TO: Oracle Database - Enterp ...
- py-faster-rcnn 训练参数修改(转)
faster rcnn默认有三种网络模型 ZF(小).VGG_CNN_M_1024(中).VGG16 (大) 训练图片大小为500*500,类别数1. 一. 修改VGG_CNN_M_1024模型配置文 ...
- idea中git颜色不显示或者文件右键没有git按钮解决方法
VCS--->Enable Version Control Integration,然后选择git就可以了
- Spring-Cloud-Ribbon学习笔记(二):自定义负载均衡规则
Ribbon自定义负载均衡策略有两种方式,一是JavaConfig,一是通过配置文件(yml或properties文件). 需求 假设我有包含A和B服务在内的多个微服务,它们均注册在一个Eureka上 ...