【PAT甲级题解记录】1151 LCA in a Binary Tree (30 分)

news/2024/4/19 19:48:39/文章来源:https://blog.csdn.net/weixin_46360993/article/details/129247538

【PAT甲级题解记录】1151 LCA in a Binary Tree (30 分)

前言

Problem:1151 LCA in a Binary Tree (30 分)

Tags:树的遍历 并查集 LCA

Difficulty:剧情模式 想流点汗 想流点血 死而无憾

Address:1151 LCA in a Binary Tree (30 分)

问题描述

给定一棵二叉树,求两个元素 lowest common ancestor (LCA) 即最低公共祖先,也就是离两个元素的最近公共祖先节点。

解题思路

这道题至少可以有两种解法:

  1. 暴力并查集法:利用并查集,说是并查集,其实只是一个保存前置节点的数组,在建树的过程中就可以得到。然后对输入的元素分别向上遍历,找到第一个相同的即可,但节点的值可以为负数,若存储下标也会超范围,所以我们需要离散化,可以自己写离散化,也可以利用map直接写(实测这道题需要unorder_map才不会超时)。
  2. DFS建树法:结合建树过程(这种方法其实不用建树,但是与建树的递归逻辑重合),利用中序遍历数组的规律,当需要求的两个点分别在左右子树时则可确认当前分叉点就是LCA,否则就往两个点所在的子树dfs。可以参考柳:https://blog.csdn.net/liuchuo/article/details/82560863

排一个测试点2的坑,就是可能出现U=R的情况,此时应该输出U is an ancestor of V.

参考代码

  1. 暴力并查集法
/** @Author: Retr0.Wu* @Date: 2022-02-20 00:29:21* @Last Modified by: Retr0.Wu* @Last Modified time: 2022-02-21 20:56:14*/
#include <bits/stdc++.h>
#include <unordered_map>  // 我的万能头不包含所以我加了
using namespace std;
int N, M;
vector<int> dep_v[10010]; // 每一层有哪些值
unordered_map<int, int> pre;
unordered_map<int, int> visit;
vector<int> in_order(10010);
vector<int> pre_order(10010);
int cnt;
vector<int> ansv;
vector<int> build(int prenumber, int in_l, int in_r, int pre_l, int pre_r)
{// 在in里找pre的第一个int pos = 0;for (int i = in_l; i <= in_r; i++){if (in_order[i] == pre_order[pre_l]){pos = i;break;}}pre[pre_order[pre_l]] = prenumber;//  左子树的大小  pos-in_lif (pos > in_l){// tree[2 * root + 1] = pre_order[pre_l + 1];vector<int> tv = build(pre_order[pre_l], in_l, pos - 1, pre_l + 1, pre_l + pos - in_l);}if (pos < in_r){// tree[2 * root + 2] = pre_order[pre_l + pos - in_l + 1];vector<int> tv = build(pre_order[pre_l], pos + 1, in_r, pre_l + pos - in_l + 1, pre_r);}return ansv;
}
int main()
{//cin >> M >> N;scanf("%d %d",&M,&N);for (int i = 0; i < N; i++){scanf("%d",&in_order[i]);}for (int i = 0; i < N; i++){scanf("%d",&pre_order[i]);}build(pre_order[0], 0, N - 1, 0, N - 1);for (int i = 0; i < M; i++){int U, V;cin >> U >> V;if (pre.count(U) == 0 && pre.count(V) == 0){printf("ERROR: %d and %d are not found.\n", U, V);}else if (pre.count(U) == 0){printf("ERROR: %d is not found.\n", U);}else if (pre.count(V) == 0){printf("ERROR: %d is not found.\n", V);}else{visit.clear(); // 每一次都是新的查找公共根,必须清空int m = U, n = V;int ans = 0;int flag = 1;// 俩个点分俩条路径往上遍历,找最近的共同遍历点while (m != pre[m]){visit[m] = 1;m = pre[m];}while(n!=pre[n]){if(visit[n]==1){ans = n;flag = 0;break;}n = pre[n];}if(flag) ans = m;  // 如果最终都没有找到除树根外的最近共同祖先,ans就只有可能是树根了if (ans == U){printf("%d is an ancestor of %d.\n", U, V);}else if (ans == V){printf("%d is an ancestor of %d.\n", V, U);}else{printf("LCA of %d and %d is %d.\n", U, V, ans);}}}return 0;
}
  1. DFS建树法
#include<iostream>
#include<vector>
#include<map>
#include<cstdio>using namespace std;
int M;  // the number of pairs of nodes to be tested (1e3)
int N;  // the number of keys in the binary tree (1e4)
vector<int> pre_order, in_order;
map<int, int> pos;
int U, V;
int pos_U, pos_V; // U、V 在inorder中的位置
void init() {cin >> M >> N;in_order.resize(N), pre_order.resize(N);for (int i = 0; i < N; ++i) {cin >> in_order[i];pos[in_order[i]] = i;}for (int i = 0; i < N; ++i) cin >> pre_order[i];
}void creat_tree(int in_l, int in_r, int pre_l, int pre_r) {// find root in in_orderint pos_in = pos[pre_order[pre_l]];// LCA is found or U/V is an ancestor of anotherif ((pos_V < pos_in && pos_U > pos_in) || (pos_V > pos_in && pos_U < pos_in)) {printf("LCA of %d and %d is %d.\n", U, V, in_order[pos_in]);} else if (pos_U == pos_in) {printf("%d is an ancestor of %d.\n", U, V);} else if (pos_V == pos_in) {printf("%d is an ancestor of %d.\n", V, U);} else { // continue searchif (pos_U < pos_in && in_l < pos_in) {  // search leftcreat_tree(in_l, pos_in - 1, pre_l + 1, pre_l + (pos_in - in_l));}if (pos_U > pos_in && in_r > pos_in) {  // search rightcreat_tree(pos_in + 1, in_r, pre_l + (pos_in - in_l) + 1, pre_r);}}
}void test() {cin >> U >> V;// U/V is not foundif (pos.count(U) == 0 || pos.count(V) == 0) {if (pos.count(V) != 0) {printf("ERROR: %d is not found.\n", U);} else if (pos.count(U) != 0) {printf("ERROR: %d is not found.\n", V);} else {printf("ERROR: %d and %d are not found.\n", U, V);}return;}// LCA is found or U/V is an ancestor of anotherpos_V = pos[V], pos_U = pos[U];creat_tree(0, N - 1, 0, N - 1);
}void solution_1151() {init();for (int i = 0; i < M; i++) {test();}
}int main() {solution_1151();return 0;
}

总结

如果想要map速度不够,可以试试unordered_map。还是不行再考虑自己写映射离散化。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.luyixian.cn/news_show_74985.aspx

如若内容造成侵权/违法违规/事实不符,请联系dt猫网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【软件测试】测试老鸟的迷途,进军高级自动化测试测试......

目录&#xff1a;导读前言一、Python编程入门到精通二、接口自动化项目实战三、Web自动化项目实战四、App自动化项目实战五、一线大厂简历六、测试开发DevOps体系七、常用自动化测试工具八、JMeter性能测试九、总结&#xff08;尾部小惊喜&#xff09;前言 很多从业几年的选手…

【阿旭机器学习实战】【37】电影推荐系统---基于矩阵分解

【阿旭机器学习实战】系列文章主要介绍机器学习的各种算法模型及其实战案例&#xff0c;欢迎点赞&#xff0c;关注共同学习交流。 电影推荐系统 目录电影推荐系统1. 问题介绍1.1推荐系统矩阵分解方法介绍1.2 数据集&#xff1a;ml-100k2. 推荐系统实现2.1 定义矩阵分解函数2.2 …

消息中间件的概念

中间件(middleware)是基础软件的一大类&#xff0c;属于可复用的软件范畴。中间件在操作系统软件&#xff0c;网络和数据库之上&#xff0c;应用软件之下&#xff0c;总的作用是为处于自己上层的应用软件提供运行于开发的环境&#xff0c;帮助用户灵活、高效的开发和集成复杂的…

ICA简介:独立成分分析

1. 简介 您是否曾经遇到过这样一种情况&#xff1a;您试图分析一个复杂且高度相关的数据集&#xff0c;却对信息量感到不知所措&#xff1f;这就是独立成分分析 (ICA) 的用武之地。ICA 是数据分析领域的一项强大技术&#xff0c;可让您分离和识别多元数据集中的底层独立来源。 …

PPP简介,PPP分层体系架构,PPP链路建立过程及PPP的帧格式

PPP&#xff08;Point-to-Point Protocol&#xff09;是一种用于在两个网络节点之间传输数据的通信协议。它最初是为在拨号网络上进行拨号连接而开发的&#xff0c;现在已经被广泛应用于各种网络环境中&#xff0c;例如在宽带接入、虚拟专用网&#xff08;VPN&#xff09;等场景…

【JAVA】一个项目如何预先加载数据?

这里写目录标题需求实现AutowiredPostConstruct实例CommandLineRunner实例ApplicationListener实例参考需求 一般我们可能会有一些在应用启动时加载资源的需求&#xff0c;局部或者全局使用&#xff0c;让我们来看看都有哪些方式实现。 实现 Autowired 如果是某个类里需求某…

[1]MyBatis+Spring+SpringMVC+SSM整合

一、MyBatis 1、MyBatis简介 1.1、MyBatis历史 MyBatis最初是Apache的一个开源项目iBatis, 2010年6月这个项目由Apache Software Foundation迁移到了Google Code。随着开发团队转投Google Code旗下&#xff0c; iBatis3.x正式更名为MyBatis。代码于2013年11月迁移到Github。…

Vue中如何利用websocket实现实时通讯

首先我们可以先做一个简单的例子来学习一下简单的websocket模拟聊天对话的功能 原理很简单&#xff0c;有点像VUE中的EventBus&#xff0c;用emit和on传来传去 首先我们可以先去自己去用node搭建一个本地服务器 步骤如下 1.新建一个app.js&#xff0c;然后创建pagejson.js文…

【Linux】-- POSIX信号量

目录 POSIX信号量 sem_init - 初始化信号量 sem_destroy - 销毁信号量 sem_wait - 等待信号量&#xff08;P操作&#xff09; 基于环形队列的生产消费模型 数据结构 - 环形结构 实现原理 POSIX信号量 #问&#xff1a;什么是信号量&#xff1f; 1. 共享资源 -> 任何一…

【笔记】两台1200PLC进行S7 通信(1)

使用两台1200系列PLC进行S7通信&#xff08;入门&#xff09; 文章目录 目录 文章目录 前言 一、通信 1.概念 2.PLC通信 1.串口 2.网口 …

时间颗粒度选择(通过选择时间范围和颗粒度展示选项)

<template><div><el-time-selectplaceholder"起始时间"v-model"startTime":picker-options"startPickerOptions"change"changeStartTime"></el-time-select><el-time-selectplaceholder"结束时间&quo…

想招到实干派程序员?你需要这种面试法

技术招聘中最痛的点其实是不精准。技术面试官或CTO们常常会向我们吐槽&#xff1a; “我经常在想&#xff0c;能不能把我们项目中的代码打印出来&#xff0c;作为候选人的面试题的一部分&#xff1f;” “能不能把一个Bug带上环境&#xff0c;让候选人来试试怎么解决&#xf…

mysql中用逗号隔开的字段作查询用(find_in_set的使用)

mysql中用逗号隔开的字段作查询用(find_in_set的使用) 场景说明 在工作中&#xff0c;经常会遇到一对多的关系。想要在mysql中保存这种关系&#xff0c;一般有两种方式&#xff0c;一种是建立一张中间表&#xff0c;这样一条id就会存在多条记录。或者采用第二种方式&#xff…

【数据结构必会基础】关于树,你所必须知道的亿些概念

目录 1.什么是树 1.1浅显的理解树 1.2 数据结构中树的概念 2.树的各种结构概念 2.1 节点的度 2.2 根节点/叶节点/分支节点 2.3 父节点/子节点 2.4祖先节点/子孙节点 2.5兄弟节点 2.6树的度 2.7节点的层次 2.8森林 3. 如何用代码表示一棵树 3.1链式结构 3.1.1 树节…

Gitea Windows环境下服务搭建

前言&#xff1a;这篇文章没有去分析各大平台的优劣势&#xff0c;仅教学大家搭建一个属于自己的git代码管理器&#xff0c;主要作用在局域网内&#xff0c;办公电脑搭建一个简单的Gitea代码管理器。数据库使用SQLite3&#xff0c;环境是windows10。如果不是这个环境的话&#…

@Import注解的原理

此注解是springboot自动注入的关键注解&#xff0c;所以拿出来单独分析一下。 启动类的run方法跟进去最终找到refresh方法&#xff1b; 这里直接看这个org.springframework.context.support.AbstractApplicationContext#refresh方法即可&#xff0c;它下面有一个方法 invoke…

Node下载阿里OSS存储文件【不知目录结构】

前言&#xff1a;前端传模型ID&#xff0c;后台根据ID去阿里OSS存储下载对应文件&#xff08;不知文件内部层级结构&#xff0c;且OSS只能单个文件下载&#xff09;&#xff0c;打包成zip字节流形式返回给前端下载。 需求分析&#xff1a; 生成OSS文件关系树Node做文件下载存…

kafka(一) 的架构,各概念

Kafka架构 Kafak 总体架构图中包含多个概念&#xff1a; &#xff08;1&#xff09;ZooKeeper&#xff1a;Zookeeper负责保存broker集群元数据&#xff0c;并对控制器进行选举等操作。 &#xff08;2&#xff09;Producer&#xff1a; 生产者负责创建消息&#xff0c;将消息发…

【神经网络】LSTM为什么能缓解梯度消失

1.LSTM的结构 我们先来看一下LSTM的计算公式&#xff1a; 1.遗忘门&#xff1a; 2.输入门&#xff1a; 3.细胞状态 4.输出门 2.LSTM的梯度路径 根据LSTM的计算公式&#xff0c;可以得出LSTM的cell state与、、都存在计算关系&#xff0c;而、、的计算公式又全部都与有关&#x…

RPC异步化原理

深入RPC&#xff0c;更好使用RPC&#xff0c;须从RPC框架整体性能考虑问题。得知道如何提升RPC框架的性能、稳定性、安全性、吞吐量及如何在分布式下快速定位问题。RPC框架如何压榨单机吞吐量&#xff1f; 1 前言 TPS一直上不去&#xff0c;压测时CPU压到40%&#xff5e;50%就…