1151 LCA in a Binary Tree (30 point(s))

1151 LCA in a Binary Tree (含求LCA的通法) 目录 解法一思路 结果 解法一代码 解法一思路 1. 根据先序和中序建树 2. 对树进行深度优先遍历,找到每一个结点的父节点(注意:由于值的范围是int,直接用可能导致溢出,所以采用值在中序/先序中的下标来映射) 3. 对于读入的值,先判断有没有出现过,如果都出现过,分别得到两个结点的祖先序列(顺序是从自身到根节点),然后进行双循环比对。最后将得到的结点分别和两个输入值比较来输出结果。 结果 最后两个用例超时 解法一代码 #include<cstdio> #inclu 阅读详情

1151 LCA in a Binary Tree (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.

Given any two nodes in a binary tree, 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 binary tree, respectively. In each of the following two lines, N distinct integers are given as the inorder and preorder traversal sequences of the binary tree, respectively. It is guaranteed that the binary tree can be uniquely determined by the input sequences. 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 binary tree, 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
7 2 3 4 6 5 1 8
5 3 7 2 6 4 8 1
2 6
8 1
7 9
12 -3
0 8
99 99

Sample Output:

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

 LCA(最低公共祖先结点)。

注意点:

1. 结点的静态存储。

2. 树的重建,获得所在层次和父亲结点。

思路:

1. 先根据先序遍历和中序遍历重建树,注意保存结点的父亲结点下标和所在层次。树的存储采取静态方式,方便通过结点的值找到结点。

2. 然后根据根据给出数对的数据找到这两个结点。

3. 注意到每一个非根结点的父亲结点是唯一的。将较低的结点向上回溯,直到所在的层次一样,如果此时就相等的,说明较高的结点是较低结点的祖先;否则同时回溯,直到结点相同。

参考链接:https://blog.youkuaiyun.com/richenyunqi/article/details/79839484

#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){
	//根据前序遍历的root位置和中序遍历的left和right的区间重建树 
	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;
}

 

C++实现SM4国密算法实战指南 通过以上实现,开发者可以快速在C++项目中集成SM4国密算法,满足金融、政务等领域的安全需求。实际部署时建议通过国家密码管理局的合规性检测,并定期更新密钥管理策略。库(支持国密算法),需提前安装并配置开发环境。 阅读详情

相关推荐

Qwen3-Omni-30B-A3B-Instruct微调指南:如何打造专属智能博物官(附数据集)

本文详细介绍了如何对Qwen3-Omni-30B-A3B-Instruct模型进行微调,以打造专业的智能博物官。指南涵盖了从理解模型架构优势、构建高质量文博数据集,到使用LLaMA-Factory进行实战微调、效果评估与部署优化的完整流程,并提供了关键的环境配置与性能优化建议,帮助开发者在垂直领域高效定制专属AI助手。

weixin_29096377的博客 1144

PAT-A 1151 LCA in a Binary Tree (30 分)

树中两个结点 U 和 V 的最低公共祖先(LCA)是指同时具有 U 和 V 作为后代的最深结点。 给定二叉树中的任何两个结点,请你找到它们的 LCA。 输入格式 第一行包含两个整数 M 和 N,分别表示询问结点对数以及二叉树中的结点数量。 接下来两行,每行包含 N 个不同的整数,分别表示二叉树的中序和前序遍历。 保证二叉树可由给定遍历序列唯一确定。 接下来 M 行,每行包含两个整数 U 和 V,表示一组询问。 所有结点权值均在 int 范围内。 输出格式 对于每对给定的 U 和 V,输出一行结果。 如果 U

qq_45682511的博客 960

10分钟上手YOLOv11:最强姿态估计模型来了,代码开源,效果炸裂!

【10分钟玩转YOLOv11实时姿态估计】🚀 YOLOv11作为YOLO家族最新成员,在速度、精度和易用性上全面升级,尤其适用于姿态估计等复杂视觉任务。本文带您快速掌握: 1️⃣ 核心概念:姿态估计即识别人体17个关键点(如关节部位)并连接成数字骨架,是智能健身、体感游戏等应用的基础。 2️⃣ 技术优势: 超轻量级(最小仅2.9M参数) 极速推理(GPU仅1.7ms) 高精度(COCO mAP@50-95达50%) 3️⃣ 三步实操: ① 安装:pip install ultralytics ② 准备人物

weixin_41246802的博客 1789

【PTA Advanced】1151 LCA in a Binary Tree(C++)

1151 LCA in a Binary Tree

Trapped_beast的博客 445

深入解析APS开源排程工具OptaPlanner:链式结构与移动优化

本文深入解析了开源APS排程工具OptaPlanner的核心机制,重点阐述了链式结构如何作为排程优化的“骨架”,通过单一`previous`变量隐式定义资源与顺序,从而简化建模。文章详细介绍了基于此结构的移动操作(如`ChainChangeMove`)如何驱动优化引擎探索解空间,并自动触发影子变量更新与分数计算,为构建高效、灵活的生产计划系统提供了实践指导。

keras9composer的博客 298

逻辑回归:利用Python实现二分类算法

通过使用Python编写的代码,我们可以训练逻辑回归模型并进行预测。接下来,我们将实现逻辑回归算法的训练函数。训练函数将接受训练数据集X和对应的标签y,以及学习率alpha和迭代次数num_iterations。训练完成后,我们可以使用训练得到的参数theta来进行预测。在这个例子中,我们有一个包含4个样本的训练数据集X,每个样本有两个特征。在这个例子中,如果预测概率大于等于0.5,则预测为类别1,否则预测为类别0。现在,我们可以使用训练函数来训练模型。根据给定的样本x_new,预测函数将返回相应的类别。

XlltEditor的博客 326

1151 LCA in a Binary Tree

没怎么写过树,欠的还是要还的。 目前看了题解是两种做法,一种是直接找,另一种是先建树再LCA(LCA用的裸查找)。 直接查找 a和b的最近公共祖先一定在中序遍历a的位置和中序遍历b的位置之间,而a和b的公共祖先一定在先序遍历中a和b的位置之前。在先序遍历中从0开始查找,找到的第一个中序遍历位置位于a和b之间的就是最近公共祖先。 #include <iostream> #include <algorithm> #include <stdio.h> #include <

Erin2333的博客 323

HTML蓝色动态爱心(完整代码)

HTML语言实现蓝色动态爱心的完整代码。

AIGC 8万+

PTA 甲级真题 1151 LCA in a Binary Tree

【代码】PTA 甲级真题 1151 LCA in a Binary Tree。

weixin_52030057的博客 251

09 SAP CPI 访问公共API接口

我在网上找了一个公共API,入参输入汉字返回拼音。至此,一个真正的访问API的接口就做好了。点击保存时可看到红叉报错。

SAP CPI 477

PyTorch深度学习实战(2)——PyTorch基础

PyTorch 是广泛应用于机器学习领域中的强大开源框架,因其易用性和高效性备受青睐。在本节中,将介绍使用 PyTorch 构建神经网络的基础知识。首先了解 PyTorch 的核心数据类型——张量对象。然后,我们将深入研究用于张量对象的各种操作。PyTorch 提供了许多帮助构建神经网络的高级方法及组件,并提供了利用 GPU 更快地训练神经网络的张量对象。

盼小辉丶的博客 5万+

【PAT】A1151 LCA in a Binary Tree (30point(s))

文章目录A1151 LCA in a Binary Tree (30point(s))Input Specification:Output Specification:Sample Input:Sample Output:CodeAnalysis Author: CHEN, Yue Organization: 浙江大学 Time Limit: 1000 ms Memory Limit: 64 MB...

Duke 247

【CT/电镜 重建教程 1.1】层析成像的精确实空间迭代重建算法(RESIRE)

在此,我们报告了RESIRE算法的发展用于准确的断层重建。与实空间迭代算法相比,RESIRE使用傅里叶切片定理或Radon变换作为正向投影,使用线性变换作为反向投影,两者都具有高精度。与EST和GENFIRE相比,RESIRE是一种实空间迭代算法,可以在并行计算中实现快速运行时间。

2401_87064292的博客 1062

1151 LCA in a Binary Tree (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. Given any two nodes in a binary tree, you are supposed to find their LCA....

qq_41196737的博客 185

数学建模模型总结

这是本人在两年内准备参加数学建模竞赛搜集到的模型资料,已经经过了本人的筛选和整理,希望能够对准备参加数学建模竞赛的学弟学妹们提供一些帮助!

计算机控制技术及应用

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

PAT甲级1151 LCA in a Binary Tree (30point(s))

求 LCA,当然,不用真写个 LCA 算法出来(tarjan 什么的)。题目给了树的中序遍历和先序遍历,我们不需要先把树建起来,而是可以在建树的过程中,找出两个结点的 LCA。 具体的想法是,当两个结点不同时,它们必分布在它们的 LCA 的两侧,结合给出的中序遍历,分布在两侧的结点在中序遍历中相对根结点(即 LCA)的位置也正好是一前一后。根据这个性质,我们就可以按照建树的方法,先在前序遍历中找根,然后在中序遍历中把两棵子树找出来,看看两个结点是否分布在两棵不同的子树上。反复查找直到找到满足的情况,第一.

坤斤拷的博客 281

ssm框架搭建

这个是一个在idea上成功跑起来的ssm框架项目,保存在这里方便以后使用直接下载

1151 LCA in a Binary Tree(30 point(s))

1151 LCA in a Binary Tree

张蔡辉 1408

1151 LCA in a Binary Tree (30point(s)) - C语言 PAT 甲级

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 and V as descendants. Given any two nodes in a binary tree, you are supposed to find their LCA. Input Specification:

有三只土豆 196

PAT_甲级_1151 LCA in a Binary Tree (30point(s)) (C++)【LCA倍增法】

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

&再见萤火虫&的博客 251

1151 LCA in a Binary Tree (30 point(s))找共同祖先同一道408真题思路!!

这个共同祖先像不像那道找shared链表算法真题!不过真题答案只找到了公共值结点就算后边一致,万一后边不是共同后缀呢,感觉还得用栈,从最后一个元素开始弹出! 代码思路: 手工先序建立遍历栈:分别到u,v停止 然后相当于那道真题,弹出第一个相同元素的是LCA #include<iostream> #include<stack> using namespace std; int in[1001],pre[1001],u,v,tagu=0,tagv=0; struct node{

qq_42835526的博客 171

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 and V as descendants. Given any two nodes in a binary tree, you are supposed to find their LCA. In...

请赐教 334

pta--1151 LCA in a Binary Tree(30 分)(二叉树 LCA 模板题)

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 and V as descendants. Given any two nodes in a binary tree, you a...

来啦(#^.^#) 1254

PAT 甲级 1151  LCA in a Binary Tree

1151LCA in a Binary Tree(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. Given any two nodes in a binary tree...

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

[C++] PAT 1151 LCA in a Binary Tree (30分)

Sample Input: 6 8 7 2 3 4 6 5 1 8 5 3 7 2 6 4 8 1 2 6 8 1 7 9 12 -3 0 8 99 99 Sample Output: LCA of 2 and 6 is 3. 8 is an ancestor of 1. ERROR: 9 is not found. ERROR: 12 and -3 are not found. ERROR: 0 is not found. ERROR: 99 and 99 are not found. 题解: .

快乐de约定的博客 1999

1151 LCA in a Binary Tree (30分) 测试点 2

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 and V as descendants. Given any two nodes in a binary tree, you are supposed to find their LCA. Input Specification: Ea.

小渣渣的博客 635

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 and V as descendants. Given any two nodes in a binary tree, you are supposed to find their LCA. In...

huantonglei的博客 262
上一篇: 1127 ZigZagging on a Tree (30 point(s))
下一篇: 1143 Lowest Common Ancestor (30 point(s))
coderwait
博客等级 码龄9年 20粉丝 210原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值