题目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. [Python设计模式] 第19章 分公司=部门?——组合模式

    github地址:https://github.com/cheesezh/python_design_patterns 组合模式 组合模式,将对象组合成树形结构以表示"部分-整体" ...

  2. 优麒麟 16.04 LTS(长期支持)版本

    Ubuntu Kylin (中文又被称为优麒麟)是基于Ubuntu的一款官方衍生版. 它是一款专门为中国市场打造的免费操作系统.它包括Ubuntu用户期待的各种功能,并配有必备的中文软件及程序. ht ...

  3. iOS实现图片裁剪功能,基于TKImageView完善与讲解

    1.功能需求:需要实现图片区域裁剪功能. 2.效果图:     3.实现原理:本来想自己实现的,刚好看到一个比较好的库:TKImageView,下载好研究了下,发现基本都能满足我的需求,而且封装的也比 ...

  4. [转]tensorflow中的gather

    原文链接 tensorflow中取下标的函数包括:tf.gather , tf.gather_nd 和 tf.batch_gather. 1.tf.gather(params,indices,vali ...

  5. fastcgi php-cgi与php-fpm区别和之间的关系

    关于FastCGI.php-cgi.php-fpm的区别是什么,各自有什么用途,以及相互间的关系是什么,查阅相关资料,可谓是众说纷纭,莫衷一是: 说法一:fastcgi是一个协议,php-fpm实现了 ...

  6. 同一个tomcat下面放多个项目 每个项目用不同的域名访问

    vim ./conf/server.conf <Host name=" appBase="/www/test1/webapps" ##这是war包存放的位置 unp ...

  7. Sublime text 3 格式化代码 插件

    JsFormat: 重新打开sublime就能使用js格式化插件 使用方法: 1.快捷键:ctrl+alt+f 2.或者先用快捷键打开命令面板 “ctrl + shift + p”, 再输入 “For ...

  8. postMessage使用方法

    1.子页面向父页面发送消息 var parentData = {type: 'passDataBack', data: passData}; window.parent.postMessage(par ...

  9. shell 十进制数字转十六进制字符串并将结果保存到变量

    . . . . . 今天写测试脚本的时候需要将生成的十六进制值作为参数传递给某个命令,而循环生成的数值都是十进制的.在网上查了好久也没有找到如何将一个变量中的值进行进制转换,并保存到变量中,网上的办法 ...

  10. SAP FICO 凭证导入接口 数据xml格式

    接口传入参数说明 <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xm ...