1151 LCA in a Binary Tree (30 分)

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

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.

这题写了1小时14分钟。。。

根据前序中序递归建树。用set判断点是否存在。如果都在。分别求出两个点从根到他们的各自路径。从较长的路径中查找2个点是否都在。都在则输出 X is an ancestor of Y. 否则逐个比较路径,分叉口就是LCA。比如2的路径(5372),6的路径(536),那么分叉口就是3,LCA就是3.

 

又看了下其他大佬的思路,简直吊打我

 

#include<stdio.h>
#include<vector>
#include<set>
#include<stdlib.h>
using namespace std;
int pre[10010],in[10010];
set<int>s;
vector<int>path1,path2,path,tmp;
typedef struct Node* node;
struct Node{
	int x;
	node left;
	node right;
};
node build(int s1,int e1,int s2,int e2){
	node T=(node)malloc(sizeof(struct Node));
	T->x=pre[s1];
	T->left=T->right=0;//NULL
	int root,i;
	for(i=s2;i<=e2;i++){
		if(in[i]==pre[s1]){
			root=i;break;
		}
	}
	if(root!=s2){
		T->left=build(s1+1,root-s2+s1,s2,root-1);
	}
	if(root!=e2){
		T->right=build(root-s2+s1+1,e1,root+1,e2);
	}
	return T; 
}
void dfs(node T,int c){
	if(T){
		tmp.push_back(T->x);
		if(T->x==c){
			path=tmp;
			tmp.pop_back();//不要忘了!!! 
			return;//不return 最后2点超时 
		}
		dfs(T->left,c);
		dfs(T->right,c);
		tmp.pop_back();
	}
}
int main(){
	int n,m,i,c1,c2,index1,index2;
	scanf("%d %d",&m,&n);
	for(i=0;i<n;i++){
		scanf("%d",&in[i]);//中序 
		s.insert(in[i]);//集合便于后面查找是否存在 
	}
	for(i=0;i<n;i++){
		scanf("%d",&pre[i]);//前序 
	}
	node T=build(0,n-1,0,n-1);//建树 
	while(m--){
		scanf("%d %d",&c1,&c2);
		if(s.find(c1)==s.end()&&s.find(c2)==s.end()){//都不在 
			printf("ERROR: %d and %d are not found.\n",c1,c2);
		}
		else if(s.find(c1)==s.end()){//有一个不在 
			printf("ERROR: %d is not found.\n",c1);
		}
		else if(s.find(c2)==s.end()){//有一个不在 
			printf("ERROR: %d is not found.\n",c2);
		}
		else{
			tmp.clear();//74行用了tmp,所以下次dfs前clear 
			dfs(T,c1);
			path1=path;//保存c1路径 
			dfs(T,c2);
			path2=path;//保存c2路径 
			tmp=path1.size()>path2.size()?path1:path2;
			index1=index2=-1;
			for(i=0;i<tmp.size();i++){
				if(tmp[i]==c1){
					index1=i;
				}
				if(tmp[i]==c2){
					index2=i;
				}
			}
			if(index1!=-1&&index2!=-1){//两个点都在同一路径 
				if(index1>index2){
					swap(c1,c2);//不用#include<algorithm>?? 
				}
				printf("%d is an ancestor of %d.\n",c1,c2);
			}
			else{//逐个比较,不同的分叉点就是LCA 
				for(i=0;i<path1.size()-1&&i<path2.size()-1;i++){
					if(path1[i]==path2[i]&&path1[i+1]!=path2[i+1]){
						printf("LCA of %d and %d is %d.\n",c1,c2,path1[i]);
						break;
					}
				}
			}
		}
	}
} 

 

SAP成本计划BAPI接口BAPI_COSTACTPLN_POSTPRIMCOST深度解析与实战指南 在SAP ERP系统中,成本计划是财务与控制模块的核心功能,它涉及对企业未来成本支出的预测与规划。其原理是通过结构化数据模型,将成本要素配到具体的成本对象上,以实现预算编制和成本控制。这一技术的核心价值在于实现成本数据的标准化、自动化处理,从而提升财务流程效率与数据准确性。在应用场景上,成本计划广泛应用于年度预算编制、项目成本预测、系统间数据集成以及历史数据迁移等关键业务环节。本文聚焦于实现上述流程自动化的关键技术——SAP标准接口BAPI_COSTACTPLN_POSTPRIMCOST,并深入探讨其在高 阅读详情

相关推荐

新手福利!大龙虾OpenClaw可视化管理工具来了!告别命令行,这个可视化工具让OpenClaw管理变得超简单

还在用命令行管理OpenClaw?🚀 这个跨平台可视化工具支持14+AI模型,一键切换,还能对接微信飞书钉钉等8大消息渠道,让AI助手管理变得简单直观 ✅ 暗色主题护眼舒适,Rust后端高性能低占用,新手也能轻松上手 🎯

XiaoqiangClub的博客 705

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的博客 957

chromedriver-linux64-138.0.7204.49(Stable).zip

chromedriver-linux64-138.0.7204.49(Stable).zip

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

1151 LCA in a Binary Tree

Trapped_beast的博客 442

1151 LCA in a Binary Tree

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

Erin2333的博客 321

PTA 甲级真题 1151 LCA in a Binary Tree

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

weixin_52030057的博客 246

PAT 1151 LCA in a Binary Tree30 )- 甲级

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...

柳婼 の blog 9100

1151 LCA in a Binary Tree (30 )

首先是鬼鬼珊的闲聊一钟 今天给大家扯一点什么呢?好吧,最近的论文看得我想哭,最悲伤的事是看不懂呀,尤其是国外研究的论文,一个句子长到我还要进行英文的长难句解析才勉强搞得清楚其中的深刻含义,导致我每天都想躺尸。说起来还有一件事值得一提,很久之前和朋友聊天,说起恋爱这个事,她以一个过来人的经验告诉我,在恋爱中尽量不要作,不要乱发脾气。因此,我为了促进自我良好的恋爱发展进行了深刻的调研,调研平...

超级酷的鬼鬼珊 517

1151 LCA in a Binary Tree30

1151LCA in a Binary Tree30 ) 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...

liuxinghui_SJTU的博客 292

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. I...

小白的博客 355

1151 LCA in a Binary Tree(《算法笔记》版)

1151 LCA in a Binary Tree(《算法笔记》版)

weixin_42589381的博客 294

pat1151 LCA in a Binary Tree

题意:输入一颗二叉树的中序,先序,q个询问,每次询问两个节点的lca。 思路:求dfs序,st表维护dfs序中的高度,map映射一下每个值对应的节点序号,rmq求lca。预处理查询 代码 #include &lt;iostream&gt; #include &lt;cstdio&gt; #include &lt;cstring&gt; #include &lt;algorithm&gt; ...

cccsssxxx 671

PAT 1151 LCA in a Binary Tree

这一题给出两个节点,找出在二叉树上的最近的公共祖先。 开头一想可以先建树然后保存路径找结果的方法,后来觉得这样代码量会很大,而且短时间写不出来,可能存在时间超限的风险。 改为类似于并查集的思维,用元素的下标指向父节点的下标,用的是先序数组的下标,先用先序遍历和中序遍历按照建树的思维对将元素的下标指向父节点的下标,完成一棵"反向树"。先用map来判断元素是否在树中,然后用两个节点的下标去查找结果,先...

Genius_J的博客 253

LCA in a Binary Tree

给出一棵树,求两个节点的最近公共祖先。 每次让两个点中深度大的点向上跳,直到两个点相交。很明显,当数据是一条链时,每次询问的时间复杂度可达到O(n)。 对于此题是采用的lca算法。 对于一棵普通的二叉树 The lowest common ancestor (LCA) of two nodes U and V in a tree is the deepest node that has b...

策马奔腾向前冲 274
上一篇: 1155 Heap Paths (30 分)
下一篇: LCA查找最近公共祖先
石前有座桥
博客等级 码龄10年 46粉丝 387原创
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值