习题4.5 顺序存储的二叉树的最近的公共祖先问题 (浙大《数据结构》第2版)

传送门:https://pintia.cn/problem-sets/434/problems/6180

提示:顺序存储得到的二叉树是完全二叉树,利用完全二叉树性质解题即可。

AC代码:

#include
#include
using namespace std;

int n,x,y;
int a[2000];

int main(){
	cin>>n;
	for(int i=1;i<=n;i++)scanf("%d",&a[i]);
	scanf("%d%d",&x,&y);
	if(a[x]==0)printf("ERROR: T[%d] is NULL", x);
	else if(a[y]==0)printf("ERROR: T[%d] is NULL", y);
	else{
		while(1){
			if(x==y){
				cout<=y) x/=2;
				else y/=2;
			}
		}
	}
	return 0;
}

 

你可能感兴趣的:(习题4.5 顺序存储的二叉树的最近的公共祖先问题 (浙大《数据结构》第2版))