1143 Lowest Common Ancestor (30 point(s))

【Linux】常见指令的使用 Linux常用指令 阅读详情

1143 Lowest Common Ancestor (30 point(s))

The lowest common ancestor (LCA) of two nodes U and V in a tree is the deepest node that has both U and V as descendants.

A binary search tree (BST) is recursively defined as a binary tree which has the following properties:

  • The left subtree of a node contains only nodes with keys less than the node's key.
  • The right subtree of a node contains only nodes with keys greater than or equal to the node's key.
  • Both the left and right subtrees must also be binary search trees.

Given any two nodes in a BST, you are supposed to find their LCA.

Input Specification:

Each input file contains one test case. For each case, the first line gives two positive integers: M (≤ 1,000), the number of pairs of nodes to be tested; and N (≤ 10,000), the number of keys in the BST, respectively. In the second line, N distinct integers are given as the preorder traversal sequence of the BST. Then M lines follow, each contains a pair of integer keys U and V. All the keys are in the range of int.

Output Specification:

For each given pair of U and V, print in a line LCA of U and V is A. if the LCA is found and A is the key. But if A is one of U and V, print X is an ancestor of Y. where X is A and Y is the other node. If U or V is not found in the BST, print in a line ERROR: U is not found. or ERROR: V is not found. or ERROR: U and V are not found..

Sample Input:

6 8
6 3 1 2 5 4 8 7
2 5
8 7
1 9
12 -3
0 8
99 99

Sample Output:

LCA of 2 and 5 is 3.
8 is an ancestor of 7.
ERROR: 9 is not found.
ERROR: 12 and -3 are not found.
ERROR: 0 is not found.
ERROR: 99 and 99 are not found.

类似1151。根据BST前序重建树、

只需要注意:

1. BST的中序遍历是非递减的。

2. 结点的静态存储是基于前序或者后序的。

#include<iostream>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
int M,N;
const int MAX = 1e4+7;
struct Node{
	int data;int level;int father;
	Node(int d=0,int l=0,int f=-1):data(d),level(l),father(f){}
};
Node pre[MAX];
int in[MAX];
void createTree(int root,int left,int right,int father,int level){
	if(left>right) return;
	pre[root] =  Node(pre[root].data,level,father);
	int index = left;
	while(pre[root].data!=in[index]) index++;
	createTree(root+1,left,index-1,root,level+1);
	createTree(root+1+index-left,index+1,right,root,level+1);
}
int main(void){
	cin>>M>>N;
	for(int i=0;i<N;i++) cin>>in[i];
	for(int i=0;i<N;i++) cin>>pre[i].data;
	createTree(0,0,N-1,-1,0);
	int a,b;
	for(int i=0;i<M;i++){
		cin>>a>>b;
		int ai=N;int bi=N;
		for(int i=0;i<N;i++){
			if(a==pre[i].data) ai = i;
			if(b==pre[i].data) bi = i;
		}
		if(ai==N&&bi==N) printf("ERROR: %d and %d are not found.\n",a,b);
		else if(ai==N&&bi!=N) printf("ERROR: %d is not found.\n",a);
		else if(ai!=N&&bi==N) printf("ERROR: %d is not found.\n",b);
		else{
			bool deeper = true;//Node a is deeper;
			if(pre[ai].level<pre[bi].level){
				deeper = false;
				swap(ai,bi);
			}
			while(pre[ai].level!=pre[bi].level) ai = pre[ai].father;
			if(ai==bi){
				printf("%d is an ancestor of %d.\n",(deeper?b:a),(deeper?a:b));
			}
			else{
				while(ai!=bi){
					ai = pre[ai].father;
					bi = pre[bi].father;
				}
				printf("LCA of %d and %d is %d.\n",a,b,pre[ai].data);
			}
		}
	}
	return 0;
}

 

Unity零基础到入门 ☀️| 【UI系统学习】轻松学会 Unity 的 UGUI基础控件的使用(包括一个简易血条、蓝条设计) Unity,UGUI。本篇博客来挑几种Unity中最常用的UI控件如何使用和使用技巧,包括一个简易血条蓝条的小实例 阅读详情

相关推荐

Polyworks脚本开发实战指南:高效生成数据彩图与报告的关键指令解析

本文深入解析了Polyworks脚本开发中生成数据彩图与自动化报告的核心指令。通过实战案例,详细介绍了REFERENCE_SURFACE、THICKNESS和CLEARANCE等关键彩图生成指令的正确用法与常见避坑点,并阐述了如何利用REPORT_ITEM命令实现从截图、表格生成到智能排版的全流程自动化,旨在帮助三维检测与质量控制工程师大幅提升工作效率与报告标准化水平。

weixin_29209825的博客 556

1143 Lowest Common Ancestor (30 point(s))

题解 利用BST的性质即可。 #include&lt;iostream&gt; #include&lt;cstdio&gt; #include&lt;vector&gt; #include&lt;unordered_map&gt; using namespace std; int w, u, v, n, m; unordered_map&lt;int, bool&gt; have; in...

徐伯莱 326

华为OD机试真题 2026-9-2【查找幸运数】

华为OD机试真题 新系统【查找幸运数】,提供C语言、Java、C++、Python、Go、JavaScript源码实现,并提供完整思路讲解,以及在线OJ模拟做题。华为OD机试真题 【查找幸运数】多语言题解。

qq_45776114的博客 501

PAT_甲级_1143 Lowest Common Ancestor (30point(s)) (C++)【BST构建/寻找LCA/倍增法】

目录 1,题目描述 题目大意 输入 2,思路 数据结构 算法 3,AC代码 4,解题过程 1,题目描述 Sample Input: 6 8 6 3 1 2 5 4 8 7 2 5 8 7 1 9 12 -3 0 8 99 99 Sample Output: LCA of 2 and 5 is 3. 8 is an ancestor of 7. ERROR:...

&再见萤火虫&的博客 379

数学建模学习(121):Python实现模糊AHP(Fuzzy AHP)——从原理到实践

Python实现模糊AHP(Fuzzy AHP)

全栈川川 1542

uni-app - 在纯 JS 文件中调用自定义弹框组件 / 封装全局 API 调用弹框组件(解决小程序、APP 无法使用 document.body.appendChild 插入组件节点)适配全端

uni-app-在纯JS文件中调用自定义弹框组件/封装全局API调用弹框组件(解决小程序、APP无法使用document.body.appendChild插入组件节点)适配全端,uni-appappendChild怎么向小程序中插入节点,如何开发类似uni.showModal全局组件。h5端可以通过document.body.appendChild挂载,请问app跟微信小程序怎么弄,请问uniapp如何封装全局api调用弹窗组件?uniapp怎么设置所有页面共用的组件Uni

🏆 前端领域优质创作者 1万+

PAT 甲级1143 Lowest Common Ancestor (30 分,一个测试点未过)

The lowest common ancestor (LCA) of two nodes U and V in a tree is the deepest node that has both U and V as descendants. A binary search tree (BST) is recursively defined as a binary tree which has ...

qq_38846633的博客 561

把数据库算力拉满的 ABAP CDS Table Function:从语法、权限到可运行样例的全景解析

文章摘要: 本文针对ABAP开发者探讨了CDS Table Function的应用场景与实现细节。当CDS View无法处理复杂计算(如窗口函数、递归等)时,可通过Table Function将逻辑下推到HANA数据库执行。关键点包括:1)CDS实体声明与AMDP实现的协作关系;2)多租户环境下的Client处理机制(@ClientHandling.type注解);3)激活与传输的阶段性特点可能导致的"暂不可用"问题;4)基于SFLIGHT的完整示例,展示如何定义Client依赖函数、实

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 308

1143 Lowest Common Ancestor(30 分)

思路:因为是BST,所以排序后的节点序就是中序遍历,然后就可以根据中序遍历和先序遍历构造BST 然后就是找公共祖先了,首先排序节点不存在的情况 然后从根节点开始找,因为是BST,所以当两个节点分别属于左右两个分支时,即找到了最低公共祖先 #include&lt;iostream&gt; #include&lt;vector&gt; #include&lt;cstring&gt; #inc...

程序人生 480

【DexGraspNet与多指手抓取算法详解】第三章 DexGraspNet数据集构建机理

抓取姿态描述多指手与物体的相对空间关系,包含手掌位姿与手指关节配置。

VectorShift的博客 498

2026年大数据开发工程师常见面试选择题

大数据面试高频考点解析 摘要:本文汇总了30道大数据开发面试高频题目,涵盖Hadoop生态、Spark、Flink、Kafka等核心组件。重点包括:1)HDFS架构与特性(5V特征、副本机制);2)MapReduce/Spark原理(Shuffle过程、RDD抽象);3)实时计算(Kafka消费组、Flink Watermark);4)数据仓库设计(分层模型、性能优化)。特别强调数据倾斜、小文件问题等实战场景的解决方案,以及从日志、资源等多维度排查任务异常的方法。掌握这些基础原理和典型问题应对策略,是应对大

不务正业的猿 452

PAT 1143 Lowest Common Ancestor (30分)

原文链接:我的个人博客 原题描述 The lowest common ancestor (LCA) of two nodes U and V in a tree is the deepest node that has both U and V as descendants. A binary search tree (BST) is recursively defined as a bina...

我是没有梦想的小白 373

什么是 SAP PCE?

SAP Private Cloud Edition 是一种基于云计算的部署模式,它结合了公有云的灵活性和私有云的安全性,为企业提供了量身定制的云服务。在 PCE 环境中,SAP 托管和维护客户的应用系统(如 SAP S/4HANA、SAP SuccessFactors 或 SAP BW/4HANA),同时客户拥有更高的定制化权限。专属环境:每个客户在云中拥有独立的资源,这保障了更高的安全性和性能。全面托管:SAP 提供基础架构、系统管理和更新服务,降低了客户的运维成本。灵活扩展。

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 2250

1143 Lowest Common Ancestor (30 point(s))

思路: 1. 对 BST 的先序排列,进行递增排序,得到中序排列,利用先序+中序,可得整棵 BST 2. 如果在中序排列中,根结点处于X和Y之间,那么根节点就是 LCA,否则在左子树,或者右子树 1143 Lowest Common Ancestor (30 point(s)) The lowest common ancestor (LCA) of two nodes U and V in a tree is the deepest node that has both U and V as des

专栏 271

ET2012新版免狗免安装绿色软件

ET2012新版免狗免安祖昂绿色软件。,新界面,新视觉,

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

PAT甲级1143 Lowest Common Ancestor (30point(s))

这题比 1151 简单一点,因为给出的是 BST,它的中序遍历是非减的,从而在判断两结点是否在当前根结点的两侧时,可以直接比较大小来判断。 比较坑的是,必须要先找到合适的范围,然后输出答案,而不能边遍历(或递归)边判断是否该输出,会有个点超时。我真的想不明白就这一组 If else 判断居然能超时。 #include <bits/stdc++.h> #define INF 0x3f3f3f3f using namespace std; const int maxn = 1e4+4; in.

坤斤拷的博客 354

visio安装包

Microsoft Office Visio 是一款专业的制图软件。利用经过更新的形状、协作工具和数据链接图表,创建专业级图表以简化复杂的信息。

1143 Lowest Common Ancestor (30point(s)) - C语言 PAT 甲级

* 先遍历一遍看看所给节点是否在二叉树中 * 最低公共节点表示,两节点一定分别在最低公共节点左子树和右子树中 * 因为是二叉搜索树,所以两节点是否在某节点的左右子树,可以用节点值的大小判断

有三只土豆 191

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

1143. Lowest Common Ancestor (30) 最近公共祖先

1143. Lowest Common Ancestor (30)时间限制200 ms内存限制65536 kB代码长度限制16000 B判题程序Standard作者CHEN, YueThe lowest common ancestor (LCA) of two nodes U and V in a tree is the deepest node that has both U and V as ...

zhangchaosd的博客 763

1143 Lowest Common Ancestor (30 分)

1143Lowest Common Ancestor(30分) The lowest common ancestor (LCA) of two nodes U and V in a tree is the deepest node that has both U and V as descendants. A binary search tre...

weixin_30828379的博客 182

PAT 1143. Lowest Common Ancestor (30) – 甲级

The lowest common ancestor (LCA) of two nodes U and V in a tree is the deepest node that has both U and V as descendants.A binary search tree (BST) is recursively defined as a binary tree which has th...

柳婼 の blog 2993

PAT A1143 Lowest Common Ancestor (30分) (平衡二叉树中的最低公共祖先)

PAT甲级:A1143 Lowest Common Ancestor (30分) The lowest common ancestor (LCA) of two nodes U and V in a tree is the deepest node that has both U and V as descendants. A binary search tree (BST) is recursi...

只想用最简短的代码刷题 316

PAT Advanced 1143 Lowest Common Ancestor (30 )

PAT Advanced 1143 Lowest Common Ancestor (30 )题目描述Input Specification:Output Specification:Sample Input:Sample Output:解题思路Code 题目描述 The lowest common ancestor (LCA) of two nodes U and V in a tree is t...

gq__97的博客 259

PTA 1143 Lowest Common Ancestor (30分)

题目地址:Lowest Common Ancestor 思路: 根据二叉搜索树的特点,其中序遍历就是从小到大的排列,所以可以根据中序遍历加前序遍历构建二叉树。 从根节点开始dfs得到每个节点的深度及父亲。 利用LCA算法不断的找最近公共祖先即可。 可以利用set查找元素。 代码实现: #include <bits/stdc++.h> using namespace std; typedef long long LL; typedef pair<int,int> PII; type

AAmiu的博客 195

PAT 甲级 1143  Lowest Common Ancestor

1143Lowest Common Ancestor(30 point(s)) The lowest common ancestor (LCA) of two nodes U and V in a tree is the deepest node that has both U and V as descendants. A binary search tree (BST) is recu...

漫浸天空的雨色的备忘录~ 760

1143. Lowest Common Ancestor (30)

1143. Lowest Common Ancestor (30)时间限制200 ms内存限制65536 kB代码长度限制16000 B判题程序Standard作者CHEN, YueThe lowest common ancestor (LCA) of two nodes U and V in a tree is the deepest node that has both U and V as ...

石前有座桥的博客 623

1143 Lowest Common Ancestor

The lowest common ancestor (LCA) of two nodes U and V in a tree is the deepest node that has both U and V as descendants. A binary search tree (BST) is recursively defined as a binary tree which has t...

无名小白的博客 261

1143 Lowest Common Ancestor (30分)

题目 The lowest common ancestor (LCA) of two nodes U and V in a tree is the deepest node that has both U and V as descendants. A binary search tree (BST) is recursively defined as a binary tree which ha...

小白的博客 383
上一篇: 1151 LCA in a Binary Tree (30 point(s))
下一篇: 1135 Is It A Red-Black Tree (30 point(s))
coderwait
博客等级 码龄9年 20粉丝 210原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值