PAT A1151 LCA in Binary Tree
利用树的前序和中序递归判定最小公共祖先~
直接根据两个序列递归处理~
#include<bits/stdc++.h>
using namespace std;
const int maxn=;
int N,M;
int pre[maxn],in[maxn];
unordered_map<int,int> pos;
void lca (int inL,int inR,int preRoot,int a,int b) {
if (inL>inR) return;
int inRoot=pos[pre[preRoot]];
int aIn=pos[a];
int bIn=pos[b];
if ((aIn>inRoot&&bIn<inRoot)||(aIn<inRoot&&bIn>inRoot))
printf ("LCA of %d and %d is %d.\n",a,b,in[inRoot]);
else if (aIn>inRoot&&bIn>inRoot)
lca (inRoot+,inR,preRoot+inRoot-inL+,a,b);
else if (aIn<inRoot&&bIn<inRoot)
lca (inL,inRoot-,preRoot+,a,b);
else if (aIn==inRoot)
printf ("%d is an ancestor of %d.\n",a,b);
else if (bIn==inRoot)
printf ("%d is an ancestor of %d.\n",b,a);
}
int main () {
scanf ("%d %d",&M,&N);
for (int i=;i<=N;i++) {
scanf ("%d",&in[i]);
pos[in[i]]=i;
}
for (int i=;i<=N;i++)
scanf ("%d",&pre[i]);
int a,b;
for (int i=;i<M;i++) {
scanf ("%d %d",&a,&b);
if (pos[a]==&&pos[b]==)
printf ("ERROR: %d and %d are not found.\n",a,b);
else if (pos[a]==)
printf ("ERROR: %d is not found.\n",a);
else if (pos[b]==)
printf ("ERROR: %d is not found.\n",b);
else lca (,N,,a,b);
}
return ;
}
也可以根据树建图,跑lca算法
#include<bits/stdc++.h>
using namespace std;
const int maxn=;
struct node {
int data;
node * left;
node * right;
};
int M,N;
int pre[maxn],in[maxn];
unordered_map<int,int> pos;
int father[maxn*];
int depth[maxn*];
node * create (int preL,int preR,int inL,int inR) {
if (preL>preR) return NULL;
node * root=new node;
root->data=pre[preL];
int k;
for (k=inL;k<=inR;k++)
if (in[k]==pre[preL]) break;
int numLeft=k-inL;
root->left=create(preL+,preL+numLeft,inL,k-);
if (root->left!=NULL) father[root->left->data]=root->data;
root->right=create(preL+numLeft+,preR,k+,inR);
if (root->right!=NULL) father[root->right->data]=root->data;
return root;
}
void bfs (node * root) {
queue<node *> q;
q.push(root);
depth[root->data]=;
while (!q.empty()) {
node * now=q.front();
q.pop();
if (now->left) {q.push(now->left);depth[now->left->data]=depth[now->data]+;}
if (now->right) {q.push(now->right);depth[now->right->data]=depth[now->data]+;}
}
}
void lca (int u,int v) {
int tu=u;
int tv=v;
while (depth[tu]<depth[tv]) {
tv=father[tv];
}
while (depth[tu]>depth[tv]) {
tu=father[tu];
}
while (tu!=tv) {
tu=father[tu];
tv=father[tv];
}
if (tu==u) printf ("%d is an ancestor of %d.\n",u,v);
else if (tu==v) printf ("%d is an ancestor of %d.\n",v,u);
else printf ("LCA of %d and %d is %d.\n",u,v,tu);
}
int main () {
scanf ("%d %d",&M,&N);
for (int i=;i<=N;i++) father[i]=i;
for (int i=;i<=N;i++) {
scanf ("%d",&in[i]);
pos[in[i]]=i;
}
for (int i=;i<=N;i++)
scanf ("%d",&pre[i]);
node * root=create(,N,,N);
bfs(root);
int u,v;
for (int i=;i<M;i++) {
scanf ("%d %d",&u,&v);
if (pos[u]==&&pos[v]==) printf ("ERROR: %d and %d are not found.\n",u,v);
else if (pos[u]==||pos[v]==) printf ("ERROR: %d is not found.\n",pos[u]==?u:v);
else lca (u,v);
}
return ;
}
PAT A1151 LCA in Binary Tree的更多相关文章
- PAT A1151 LCA in a Binary Tree (30 分)——二叉树,最小公共祖先(lca)
The lowest common ancestor (LCA) of two nodes U and V in a tree is the deepest node that has both U ...
- PAT A1102 Invert a Binary Tree (25 分)——静态树,层序遍历,先序遍历,后序遍历
The following is from Max Howell @twitter: Google: 90% of our engineers use the software you wrote ( ...
- PAT 甲级 1110 Complete Binary Tree
https://pintia.cn/problem-sets/994805342720868352/problems/994805359372255232 Given a tree, you are ...
- PAT 1102 Invert a Binary Tree[比较简单]
1102 Invert a Binary Tree(25 分) The following is from Max Howell @twitter: Google: 90% of our engine ...
- PAT甲级——1110 Complete Binary Tree (完全二叉树)
此文章同步发布在CSDN上:https://blog.csdn.net/weixin_44385565/article/details/90317830 1110 Complete Binary ...
- PAT 1102 Invert a Binary Tree
The following is from Max Howell @twitter: Google: 90% of our engineers use the software you wrote ( ...
- PAT甲级——A1110 Complete Binary Tree【25】
Given a tree, you are supposed to tell if it is a complete binary tree. Input Specification: Each in ...
- PAT Advanced 1110 Complete Binary Tree (25) [完全⼆叉树]
题目 Given a tree, you are supposed to tell if it is a complete binary tree. Input Specification: Each ...
- PAT_A1151#LCA in a Binary Tree
Source: PAT A1151 LCA in a Binary Tree (30 分) Description: The lowest common ancestor (LCA) of two n ...
随机推荐
- 如何在项目中新建.gitignore文件
1. 在需要创建 .gitignore 文件的文件夹, 右键选择 Git Bash 进入命令行,进入项目所在目录. 2. 输入 touch .gitignore 在文件夹就生成了一个“.gitigno ...
- Mac 配置cron
请参考:https://www.cnblogs.com/EasonJim/p/7819635.html 查看 crontab 是否启动 sudo launchctl list | grep cron ...
- 每天进步一点点------Sobel算子(2)
转载 http://blog.csdn.net/tianhai110 索贝尔算子(Sobel operator)主要用作边缘检测,在技术上,它是一离散性差分算子,用来运算图像亮度函数的灰度之近似值. ...
- jinja 语法 - 整型转字符串
大多数 jinja 相关的问题,其实查文档就解决了,但后来遇到这个问题,使得我把 jinja 官方文档,api.样例等,认真读了个遍= =. 发现没有直接的办法可以将整型转为字符串,对于需要进行字符串 ...
- CRT中国剩余定理 & Lucas卢卡斯定理
数论_CRT(中国剩余定理)& Lucas (卢卡斯定理) 前言 又是一脸懵逼的一天. 正文 按照道理来说,我们应该先做一个介绍. 中国剩余定理 中国剩余定理,Chinese Remainde ...
- JAVA基础学习(3)之循环
3循环 3.1循环 3.1.1循环 一直要做的行为进行循环 3.1.2数数字 while(){}判断是否进行 数数字:number/10 //数数字Scanner in = new Scanner(S ...
- Spring Boot 开发环境IDEA下的热部署
这个知识点忘记写了,我不是很热衷于IDEA的热部署,觉得太消耗机器性能. 1 引入 Pom <!--热部署--> <dependency> <groupId>org ...
- SQLite3约束介绍
SQLite 约束 约束是在表的数据列上强制执行的规则.这些是用来限制可以插入到表中的数据类型.这确保了数据库中数据的准确性和可靠性. 约束可以是列级或表级.列级约束仅适用于列,表级约束被应用到整个表 ...
- Vue-路由模式 hash 和 history
Vue 为了构建 SPA(单页面应用),需要引入前端路由系统,这也就是 Vue-Router 存在的意义.前端路由的核心,就在于 —— 改变视图的同时不会向后端发出请求. 创建的项目默认是hash模式 ...
- jq基础(2)
jquery的选择器 基本选择器 id选择器:$(“#id名称”); 元素选择器:$(“元素名称”); 类选择器:$(“.类名”); 通配符:* 多个选择器共用(并集) 案例代码: <html& ...