news 2026/7/14 12:03:01

PAT 1151 LCA in a Binary Tree

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
PAT 1151 LCA in a Binary Tree



这一题的大意是给出一个BST的前序遍历,让我们在这棵BST二叉树中,给出两个的点,判断这两个点在这棵二叉树的最近公共祖先是谁,这两个点可能并不在树中,也有可能给出的节点是另一个节点的祖先,我们需要针对不同情况,做出不同的判断。
因为题目给出的节点的值是在int范围内,也就是说这个值可能会很大,我们可以选择离散化,把N个值映射到1-N,这样我们就可以用int depp[10005]直接来表示某一个节点的深度了。与unordered_map<int,int>来说速度更快了,因为是BST,所以中序遍历的节点就是将所有值从小到大排序的结果。实际上不用离散化也可以吧,
可以参考这一题:

PAT 1151 LCA in a Binary Tree

但BST中序遍历是升序的,用离散化也可以在建树的过程中少写一个for循环查找中序遍历中根节点的位置。
我们可以根据中序遍历和前序遍历来建树,用哈希表来存储节点,从而可以来找任意两个节点的公共祖先。
因为只需要找公共祖先,所以我们不用存储孩子节点,只需要找到每一个节点的父亲节点,和它的深度即可。
根据前序遍历和中序遍历的建树方法已经写过多次了,这里不再赘述,在建好树后,我们就需要关注如何找两个节点的最近公共祖先。
我们只需要保证当深度相同时,两个节点都去看它的父亲节点,如果一个节点的深度比另一个节点大,那么我们先去看这个深度深的节点,同时看深度深的节点的祖先节点是否会等于另一个节点。 直到两个节点深度一样。
大致思路就是如上
完整代码如下:

#include<bits/stdc++.h>#include<iostream>usingnamespacestd;intM;intN;unordered_map<int,int>parent;vector<int>preorder;vector<int>inorder;vector<int>temp;unordered_map<int,int>last;intdeep[10005];unordered_map<int,int>mp;intbuild(intprestart,intpreend,intinstart,intinend,intd){if(prestart>preend||instart>inend){return-1;}introot=preorder[prestart];deep[root]=d;intindex=root-1;intlen=index-instart;intxl=build(prestart+1,prestart+len,instart,instart+len-1,d+1);if(xl!=-1)parent[xl]=root;intxr=build(prestart+len+1,preend,instart+len+1,inend,d+1);if(xr!=-1)parent[xr]=root;returnroot;}intmain(){cin>>M>>N;for(inti=0;i<N;i++){intx;cin>>x;preorder.push_back(x);}temp=preorder;sort(temp.begin(),temp.end());for(inti=0;i<temp.size();i++){mp[temp[i]]=i+1;last[i+1]=temp[i];temp[i]=i+1;}inorder=temp;for(inti=0;i<preorder.size();i++){preorder[i]=mp[preorder[i]];}introot=build(0,N-1,0,N-1,0);intu;intv;for(inti=0;i<M;i++){cin>>u>>v;if(!mp.count(u)&&!mp.count(v)){//如果u和v都不存在printf("ERROR: %d and %d are not found.\n",u,v);continue;}elseif(!mp.count(u)){printf("ERROR: %d is not found.\n",u);continue;}elseif(!mp.count(v)){printf("ERROR: %d is not found.\n",v);continue;}//现在要找最近公共祖先了//先把u,v离散//如果它们不再同一层,那么就需要把它们放到同一层inttempu=mp[u];inttempv=mp[v];if(tempu==tempv){printf("%d is an ancestor of %d.\n",u,v);continue;}boolflag=0;while(tempu!=root||tempv!=root){if(deep[tempu]>deep[tempv]){if(parent[tempu]==tempv){//说明v是u的ancestorflag=1;printf("%d is an ancestor of %d.\n",v,u);break;}tempu=parent[tempu];}elseif(deep[tempu]==deep[tempv]){if(parent[tempu]==parent[tempv]){flag=1;printf("LCA of %d and %d is %d.\n",u,v,last[parent[tempu]]);break;}tempu=parent[tempu];tempv=parent[tempv];}else{if(parent[tempv]==tempu){//说明u是v的ancestorflag=1;printf("%d is an ancestor of %d.\n",u,v);break;}tempv=parent[tempv];}}if(flag==0){printf("LCA of %d and %d is %d.\n",u,v,last[parent[tempu]]);}}return0;}

注意,给出的两个节点可能是同一个,我们要进行特判。

inttempu=mp[u];inttempv=mp[v];if(tempu==tempv){printf("%d is an ancestor of %d.\n",u,v);continue;}

总结:这一题就是BST建树+最近公共祖先的求法,我们用根据实际情况灵活建树。

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/7/14 11:22:07

3步搭建Flink监控系统:从零到一的Prometheus实战指南

还在为Flink集群运行状态"两眼一抹黑"而烦恼吗&#xff1f;&#x1f3af; 今天我们就来彻底解决这个运维痛点&#xff0c;用最简单的方式搭建完整的Flink监控体系。Apache Flink作为业界领先的流处理框架&#xff0c;其监控能力往往被低估&#xff0c;其实只需几个配…

作者头像 李华
网站建设 2026/7/14 13:41:44

终极指南:快速掌握Adams机械动力学仿真全流程

终极指南&#xff1a;快速掌握Adams机械动力学仿真全流程 【免费下载链接】Adams入门详解与实例李增刚.pdf分享 《Adams入门详解与实例》是由李增刚编写的&#xff0c;旨在为希望掌握Adams软件使用的读者提供一套系统的学习资料。本书深入浅出&#xff0c;不仅涵盖了Adams的基础…

作者头像 李华
网站建设 2026/7/14 6:25:43

ProComponents 企业级组件库终极指南:5分钟构建专业后台系统

ProComponents 企业级组件库终极指南&#xff1a;5分钟构建专业后台系统 【免费下载链接】pro-components &#x1f3c6; Use Ant Design like a Pro! 项目地址: https://gitcode.com/gh_mirrors/pr/pro-components 你是否曾经在开发企业级管理系统时&#xff0c;面对复…

作者头像 李华
网站建设 2026/7/13 7:36:52

利用联合体判断大小端

#include<stdio.h> //大小端函数 int is_endian() {union {int i;char c;}u;//小端则读取1&#xff1b;大端则读取0u.i 1;return u.c 1; } int main() {if (is_endian()) {printf("是小端");}else { printf("是大端"); }return 0; }

作者头像 李华
网站建设 2026/7/14 9:12:11

低代码测试平台选型与落地指南:加速测试团队效率转型

在当今快速迭代的软件开发环境中&#xff0c;软件测试团队面临着前所未有的压力&#xff1a;既要保证测试覆盖率和质量&#xff0c;又要应对日益缩短的开发周期。2025年的市场数据显示&#xff0c;超过60%的企业正积极寻求测试流程的自动化与智能化转型&#xff0c;而低代码测试…

作者头像 李华