利用树的前序和中序递归判定最小公共祖先~

直接根据两个序列递归处理~

#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的更多相关文章

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

  2. PAT A1102 Invert a Binary Tree (25 分)——静态树,层序遍历,先序遍历,后序遍历

    The following is from Max Howell @twitter: Google: 90% of our engineers use the software you wrote ( ...

  3. PAT 甲级 1110 Complete Binary Tree

    https://pintia.cn/problem-sets/994805342720868352/problems/994805359372255232 Given a tree, you are ...

  4. PAT 1102 Invert a Binary Tree[比较简单]

    1102 Invert a Binary Tree(25 分) The following is from Max Howell @twitter: Google: 90% of our engine ...

  5. PAT甲级——1110 Complete Binary Tree (完全二叉树)

    此文章同步发布在CSDN上:https://blog.csdn.net/weixin_44385565/article/details/90317830   1110 Complete Binary ...

  6. PAT 1102 Invert a Binary Tree

    The following is from Max Howell @twitter: Google: 90% of our engineers use the software you wrote ( ...

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

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

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

随机推荐

  1. 【音乐欣赏】《TIT FOR TAT》 - MYTH & ROID

    曲名:TIT FOR TAT 作者:MYTH & ROID [00:00.000] 作曲 : MYTH & ROID [00:01.000] 作词 : MYTH & ROID ...

  2. 【C语言】一元二次方程(求实根和虚根)

    求一元二次方程:ax2+bx+c=0 的根. 输入三个实数a,b,c的值,且a不等于0. #include<stdio.h> #include<math.h> int main ...

  3. CSS学习(3)样式表

    如何插入样式表 插入样式表的方法有三种: 外部样式表(External style sheet) 内部样式表(Internal style sheet) 内联样式(Inline style) 外部样式 ...

  4. django view 视图控制之数据返回的视图函数

    八.视图 view 概述:views.py定义的python函数,它接受Web请求并且返回Web响应. 有几个页面就有几个视图view user出入url地址,发送request--->urls ...

  5. 猴博士4小时讲完C语言视频教程

    猴博士4小时讲完C语言视频教程,一共有9节课. 目录结构如下: 目录:/2020030-猴博士4小时讲完C语言 [1G] ┣━━1.C语言基本语句(上)(更多资源访问:www.jimeng365.cn ...

  6. idea2019.2激活至2089年!

    上图! 激活到2089年8月,绝对够用! ​ 注意:在激活之前,无需改动 host 文件. 资料自取:链接:https://pan.baidu.com/s/1MzX5ewt6lbzHYuggP5sGE ...

  7. Python - metaclass元类(图)

    个人总结

  8. SQL Server不同服务器不同数据库间的操作

    什么是跨服务器操作? 跨服务器操作就是可以在本地连接到远程服务器上的数据库,可以在对方的数据库上进行相关的数据库操作,比如增删改查. 为什么要进行跨服务器操作 随着数据量的增多,业务量的扩张,需要在不 ...

  9. 用python计算一条射线到两个平面的交点

    前两天,一个朋友找我(半个程序猿)用python帮他写数学模型,当时的我直接是懵逼的,当听到三维啥的时候,整个人就好了,最终在周末花了3个小时把逻辑理了一遍,给小伙伴一个满意的答复了,话不多说,我来整 ...

  10. Vue-设置默认路由选中

    需求分析: 一个导航组件,需要其中一个是选中状态,并且样式呈现高亮,选中的导航对应的页面也需要展示出来. 功能实现: router-link内置有一个选中状态,当处于当前路由时,会给 router-l ...