PATA-1151 LCA in a Binary Tree
题意:根据前序和中序建立树,寻找两个点的LCA。
我在之前的博客中写了关于LCA的多种求法。 https://www.cnblogs.com/yy-1046741080/p/11505547.html。 在建树的过程中,建立深度和parent,来寻找LCA。
该题目的数据有一定的欺诈性,它给你结点数据是1-8,如果没有仔细看清题目,那么很有可能定义一个 node tree[10005]的数组,但是题目并没有说数据范围在1-10000内。
经过测试,如果你将范围定义稍微大一点,还是能全过的;
我在这里采用的就是二叉链表,使用二叉链表麻烦的一点在于,需要查找u,v两个结点,返回其指针。
我认为最好的方法就是建立一个映射表,映射输入data和node tree[]中下标的关系,就不需要进行查找。
#include<bits/stdc++.h>
using namespace std; struct node {
int data;
struct node* left, * right; //left/right=-1,表示空子树
int depth;
struct node* parent;
}; node* root; // 树的根
int pre[]; // 先序遍历序列
int in[]; // 后序遍历序列
set<int> is_visit; // 先序序列pre[preL,preR],中序序列in[inL,inR], depth/parent:for LCA;
node* BuildTree(int preL, int preR, int inL, int inR, int depth, node* parent) {
if (preL > preR) {
return NULL;
}
node* root = new node;
root->data = pre[preL];
root->depth = depth;
root->parent = parent;
int k;
for (k = inL; in[k] != pre[preL]; k++) {
;
}
root->left = BuildTree(preL + , preL + k - inL, inL, k - , depth + , root);
root->right = BuildTree(preL + k - inL + , preR, k + , inR, depth + , root); return root; // 返回
} // 寻找权值为value的结点
void find(node* root, int value,node* &t) {
if (root != NULL) {
if (root->data == value) {
t = root;
}
else {
find(root->left, value,t);
find(root->right, value,t);
}
}
} int LCA(int u, int v) {
node* uu;
find(root, u, uu);
node* vv;
find(root, v, vv);
while (uu->depth > vv->depth) {
uu = uu->parent;
}
while (uu->depth < vv->depth) {
vv = vv->parent;
}
while (uu != vv) {
uu = uu->parent;
vv = vv->parent;
}
return uu->data;
} int main() {
int N, M;
cin.sync_with_stdio(false);
cin >> N >> M;
for (int i = ; i <= M; i++) {
cin >> in[i];
is_visit.insert(in[i]);
}
for (int i = ; i <= M; i++) {
cin >> pre[i];
}
root = BuildTree(, M, , M, , NULL);
while (N--) {
int u, v;
cin >> u >> v;
bool f1 = is_visit.find(u) != is_visit.end() ? true : false;
bool f2 = is_visit.find(v) != is_visit.end() ? true : false; if (!f1 && !f2) {
cout << "ERROR: " << u << " and " << v << " are not found.\n";
}
else if (!f1) {
cout << "ERROR: " << u <<" is not found.\n";
}
else if (!f2) {
cout << "ERROR: " << v <<" is not found.\n";
}
else {
int w = LCA(u, v);
if (w == u) {
cout << u << " is an ancestor of " << v << ".\n";
}
else if (w == v) {
cout << v << " is an ancestor of " << u << ".\n";
}
else {
cout << "LCA of " << u << " and " << v << " is " << w << ".\n";
}
}
}
}
PATA-1151 LCA in a Binary Tree的更多相关文章
- PAT 1151 LCA in a Binary Tree[难][二叉树]
1151 LCA in a Binary Tree (30 分) The lowest common ancestor (LCA) of two nodes U and V in a tree is ...
- 【PAT 甲级】1151 LCA in a Binary Tree (30 分)
题目描述 The lowest common ancestor (LCA) of two nodes U and V in a tree is the deepest node that has bo ...
- PAT 甲级 1151 LCA in a Binary Tree
https://pintia.cn/problem-sets/994805342720868352/problems/1038430130011897856 The lowest common anc ...
- 1151 LCA in a Binary Tree(30 分)
The lowest common ancestor (LCA) of two nodes U and V in a tree is the deepest node that has both U ...
- PAT Advanced 1151 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 ...
- 1151 LCA in a Binary Tree
The lowest common ancestor (LCA) of two nodes U and V in a tree is the deepest node that has both U ...
- 1151 LCA in a Binary Tree (30point(s))
The lowest common ancestor (LCA) of two nodes U and V in a tree is the deepest node that has both U ...
- PAT甲级|1151 LCA in a Binary Tree 先序中序遍历建树 lca
给定先序中序遍历的序列,可以确定一颗唯一的树 先序遍历第一个遍历到的是根,中序遍历确定左右子树 查结点a和结点b的最近公共祖先,简单lca思路: 1.如果a和b分别在当前根的左右子树,当前的根就是最近 ...
- 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 ...
- PAT-1151(LCA in a Binary Tree)+最近公共祖先+二叉树的中序遍历和前序遍历
LCA in a Binary Tree PAT-1151 本题的困难在于如何在中序遍历和前序遍历已知的情况下找出两个结点的最近公共祖先. 可以利用据中序遍历和前序遍历构建树的思路,判断两个结点在根节 ...
随机推荐
- Re:萌娘百科上的黑幕实现
Re:萌娘百科上的黑幕 说明 本文所有的代码均来自萌娘百科.萌娘百科打钱! 第零段话(我想说的) 这方面不是我的专长,所以有的地方说的不对也请纠正! 我可不是萌娘百科的员工或者管理员或者收了钱 我只是 ...
- Android9.0 Settings 修改踩坑记录
问题现象 上图展示的很清楚,当系统语言为中文时,PreferenceScreen 中的折叠项 summary 描述重复显示的 bug,系统语言为英文时正常. 修改历程 先搜索 当前显示了 字符串,还真 ...
- \n不换行
\n在js中表示换行,<br/>在html中表示换行,所以如果在设置innerHtml值时使用 \n ,那么在页面上并不会显示换行,而在设置innerText值时使用 \n 就会显 ...
- 小白的linux笔记8:linux自动运行爬虫并发送提醒邮件
有了成功运行的爬虫后,希望能每天定时运行,且遇到错误时能及时发出提醒. 发出提醒 可以用mailx发出邮件做提醒.没有的话先安装Yum install mailx. 以qq邮箱为例,需要设置/etc/ ...
- 吴裕雄--天生自然 R语言数据可视化绘图(1)
par(ask=TRUE) opar <- par(no.readonly=TRUE) # make a copy of current settings attach(mtcars) # be ...
- Map的底层实现原理
一,前言 1.1,概述 现实生活中,我们常会看到这样的一种集合:IP地址与主机名,身份证号与个人,系统用户名与系统用户对象等,这种一一对应的关系,就叫做映射(K-V).Java提供了专门的集合类用 ...
- 页面上icon较多,又不想使用臃肿的结构怎么办?
[先看效果图] 例如这种排版,常规有两种情况 1.把[“ & ”]+ 白色背景切一起 2.写结构的时候复杂,例如:div>img*2 +文字标签 读到这里,可能有人说,第一种情况为什么两 ...
- 将一个Head下的Line复制到另一个Head下(ef+linq)
今天工作中有一个需求,要求将一个Item下的Line复制到另外一个Item下面 这个需求在工作中很多,按照以往的经验肯定是先delete from,然后再一条条遍历后insert into 这两天正好 ...
- 用R实现范式编程
面向函数范式编程(Functional programming) 模拟简单的随机过程 模拟一个简单的随机过程:从N~(0,1)标准正态分布中产生100个随机值,反复5次得到一个list,再以每个lis ...
- Python 一键安装全部依赖包
使用 pip requirements.txt 用来记录项目所有的依赖包和版本号,只需要一个简单的 pip 命令就能完成. pip freeze > requirements.txt 生成的文件 ...