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 本题的困难在于如何在中序遍历和前序遍历已知的情况下找出两个结点的最近公共祖先. 可以利用据中序遍历和前序遍历构建树的思路,判断两个结点在根节 ...
随机推荐
- Mumbai:1 Vulnhub Walkthrough
靶机地址: https://www.vulnhub.com/entry/mumbai-1,372/ 主机探测: 主机端口扫描: FTP 下载Note文件 TODO: Move these multip ...
- Linux忘记root用户的密码
问题: 由于长时间没有操作过自己安装的虚拟机,导致连root用户的密码都忘了,也真是没谁了. 我使用的是centos6.5,今天就记录一下,如何重置root用户的密码. 操作步骤: 步骤一: 当开机启 ...
- 邓士鹏【MySql大全】
禁止使用系统关键字: typename 1.计算两个日期的时间差函数 SELECT TIMESTAMPDIFF(MONTH,'2009-10-01','2009-09-01'); interval可是 ...
- IIS网站部署配置
1.配置Session State
- Django 表关系的创建
Django 表关系的创建 我们知道,表关系分为一对多,多对多,一对一 我们以一个图书管理系统为背景,设计了下述四张表,让我们来找一找它们之间的关系 Book与Publish表 找关系:一对多 左表( ...
- JS隐藏显示图片
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8 ...
- linux | 一次网卡故障处理
问题 在centos7系统中,设置ifcfg-eth*文件时,总会纠结NAME和DEVICE到底写哪个或哪个真实生效.这里实例演示下 这是网卡ifcfg-eth4配置文件.没写DEVICE,用的NAM ...
- Mac搭建本地服务器并映射到外网
最近在学习Html,小有进步变想着写一个浪漫的静态页面给女朋友浪漫一下,那么问题就来了,如何把我的网页让对网络一窍不通的女朋友看到,所以便想到了是用自己电脑作为服务器的想法.百度以后整理如下: 首先搭 ...
- opencv中的图像矩(空间矩,中心矩,归一化中心矩,Hu矩)
严格来讲矩是概率与统计中的一个概念,是随机变量的一种数字特征.设 x 为随机变量,C为常数,则量E[(x−c)^k]称为X关于C点的k阶矩.比较重要的两种情况如下: 1.c=0,这时a_k=E(X^k ...
- 获取Windows平台下 安装office 版本位数信息
最近在处理客户端安装程序过程,有一个需求:需要检测Windows平台下安装office 版本信息以及获取使用的office是32 位还是64 位: 当检测出office 位数为64位时,提示当前off ...