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 本题的困难在于如何在中序遍历和前序遍历已知的情况下找出两个结点的最近公共祖先. 可以利用据中序遍历和前序遍历构建树的思路,判断两个结点在根节 ...
随机推荐
- centos docker redis 安装
1.下载redis镜像 docker pull redis 2.下载redis.conf文件 https://redis.io/topics/config 这边查找自己服务器redis对应的版本文件 ...
- ELK学习004:Elasticsearch常规操作
CRUD 在我们的项目中有日志是一个必不可少的东西,但是日志的检索是一个很麻烦的事情,如每天一个日志,要找到问题就得一个一个找,并不能做到检索功能,这还算好的,如果是分布式的,每个机器都得找一遍,这种 ...
- 此Flash Player 与您的地区不相容,请重新安装Adobe Flash Player问题解决
flash29老版本安装说明: 如果你是Google Chrome 54及以上版本,那么直接安装 install_flash_player_**_ppapi.exe 即可,Chrome 能识别加载,无 ...
- clr via c# 定制特性
1,特性的应用范围:特性可应用于程序集,模块,类型,字段,方法,方法参数,方法返回值,属性,参数,泛型参数 2,利用前缀告诉编译器表明意图---下面的倾斜是必须的表明了我们的目标元素: [assemb ...
- 利用js+ajax在jsp与servlet间进行简单数据交换
直接上代码 jsp <%@ page language="java" contentType="text/html; charset=utf-8" pag ...
- iMacros 入门教程-基础函数介绍(4)
imacros的TRAY函数用法 这个函数的功能就是隐藏或显示,当执行imacros文件的时候,出现在特定标签的imacros图标 TRAY HIDE 就是隐藏图标 TRAY SHOW 就是显示图标 ...
- 部署Nexus作为docker的私有仓库
目录 Docker搭建Nexus私有仓库... 1 一.安装部署... 1 1.安装... 2 2.访问网页端... 2 二.配置使用... 2 1.创建本地仓库... 2 2.docker配置... ...
- bitset刷题记录
大佬的bitset用法小结 https://www.cnblogs.com/zwfymqz/p/8696631.html BZOJ3687简单题 题意:求子集的算术和的异或和,子集大小为n(n< ...
- Perl-统计文本中各个单词出现的次数(NVDIA2019笔试)
1.原题 2.perl脚本 print "================ Method 1=====================\n"; open IN,'<','an ...
- K8S 概述
K8S------概述 K8S,就是基于容器的集群管理平台,它的全称,是kubernetes.Kubernetes 这个单词来自于希腊语,含义是舵手或领航员.K8S是它的缩写,用“8”字替代了“ube ...