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 ...
随机推荐
- Python 树莓派 引脚
#!/usr/bin/python3 import RPi.GPIO as GPIO import time GPIO.setmode(GPIO.BCM) GPIO.setup(18, GPIO.OU ...
- Docker配置阿里云镜像源
Docker默认拉取镜像是从这里拉取(https://hub.docker.com/),拉取的速度让人...,所以是配置国内镜像源,拉取速度十分惊人 一.版本要求 Docker版本:1.10以上 二. ...
- 在线直播: .NET与物联网主流技术探秘 初识IoT!
DNT精英论坛暨.NET北京俱乐部是由资深.NET专家和社区活跃分子发起的技术论坛,以“分享.成长.合作.共赢”为原则,致力于打造一个领先的技术分享平台和成长交流生态.本次活动由aelf赞助支持,刘洪 ...
- ZOJ1002 —— 深度优先搜索
ZOJ1002 —— Fire net Time Limit: 2000 ms Memory Limit: 65536 KB Suppose that we have a square city wi ...
- PHP 超全局变量之$_FILES
$_FILES——通过 HTTP POST 方式上传到当前脚本的项目的数组. 假设我们上传文件字段name='userfile',$_FILES数组里包括: $_FILES['userfile'][' ...
- 简单桶排序(Bucket Sort)
1.基本思想 桶排序是将待排序集合中处于同一个值域的元素存放在同一个桶中1. 2.算法设计2 假设有一个班级有5个人,这次期末他们分别考了5分,2分,4分,5分,8分(满分为10分).需要将这些分数从 ...
- 关于anaconda-navigator打不开的问题
19-10版本的anaconda-navigator打不开,没有图形化界面就是很糟糕 在命令行执行各种命令都没有问题,说明anaconda并没有出现大的问题,可能只是图形化界面出了问题. 执行 ana ...
- 前端框架vue学习笔记:环境搭建
兼容性 不兼容IE8以下 Vue Devtools 能够更好的对界面进行审查和调试 环境搭建 1.nodejs(新版本的集成了npm)[npm是node包管理 node package manager ...
- 运维数据同步工具:rsync,serync,csync,drbd,info(基于文件系统)
Rsync官方站点:http://rsync.samba.org 1. Rsync rsync是一款开源的实现数据全量与增量同步备份的工具 生产环境中使用的场景: 1:一般用于数据异地备份 2:用于 ...
- Django中 from django.utils import timezone 和import datetime的区别
在现实环境中,存在多个时区,用户之间很有可能存在于不同的时区,并且许多国家都拥有自己的一套夏令时系统,所以如果网站面向的是多个时区用户,只以当前时间为标准开发,便会在时间上产生错误. 为解决这个此类问 ...