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 ...
随机推荐
- grid - 网格项目层级
网格项目可以具有层级和堆栈,必要时可能通过z-index属性来指定. 1.在这个例子中,item1和item2的开始行都是1,item1列的开始是1,item2列的开始是2,并且它们都跨越两列.两个网 ...
- JSONObject、JSONArray、Map、JavaBean的相互转换
1,JSONObject json对象,就是一个键对应一个值,使用的是大括号{ },如:{key:value} 2,JSONArray json数组,使用中括号[ ],只不过数组里面的项也是json键 ...
- 找回停掉docker的文件
如果容器还能重启,就restart, 如果状态是DEAD的话, Error response from daemon: Container is marked for removal and cann ...
- Swoole PHP windows composer
直接下载了 Swoole PHP 的 Windows 版安装包来用,遇到需要 Composer. 先是下载了 composer.phar.在这里下的 https://getcomposer.org/d ...
- CMD 命令1
cmd /c dir 是执行完dir命令后关闭命令窗口. cmd /k dir 是执行完dir命令后不关闭命令窗口. cmd /c start dir 会打开一个新窗口后执行dir指令,原窗口会关闭. ...
- WebViewJavascriptBridge 进行js 与native通信。
1, iOS端加载web页面.开启日志并给webView建立JS与OC的桥梁 - (void)viewWillAppear:(BOOL)animated { if (_bridge) { retur ...
- Hbase:原理和设计
转载自:http://www.sysdb.cn/index.php/2016/01/10/hbase_principle/ ,感谢原作者. 简介 HBase —— Hadoop Database的简称 ...
- 微信小程序——动态设置swiper的高度
根据小程序的设定,swiper组件默认高度为150px,无法根据内容来撑高.如果里面的内容固定还好说,直接设置一个高度就可以了.要是里面内容是动态变化的,这个特性使得我们使用这个组件的时候感到诸多不便 ...
- R语言 启动报错 *** glibc detected *** /usr/lib64/R/bin/exec/R: free(): invalid next size (fast): 0x000000000263a420 *** 错误 解决方案
*** glibc detected *** /usr/lib64/R/bin/exec/R: free(): invalid next size (fast): 0x000000000263a420 ...
- OSPF进程号的意义及多进程OSPF
OSPF进程号的意义及多进程OSPF—吴锦霖分享 1. OSPF进程号的概念 在配置OSPF时,我们采用的是router ospf命令,在该命令后面需要加上这个OSPF进程的进程号(Proces ...