习题4.5 顺序存储的二叉树的最近的公共祖先问题 (25 point(s))

传送门
桶排一下就好了

#include<iostream>
using namespace std;
const int N=2e3+10;
int tree[N];
int main()
{
    
    
	int n,a,b;
	cin>>n;
	for(int i=1;i<=n;i++)
		cin>>tree[i];
	cin>>a>>b;
	if(!tree[a])printf("ERROR: T[%d] is NULL",a);
	else if(!tree[b])printf("ERROR: T[%d] is NULL",b);
	else 
	{
    
    
		while(a!=b)
			{
    
    
				if(a>b)a/=2;
				else b/=2;
			}
		printf("%d %d",a,tree[a]);
	}
}

猜你喜欢

转载自blog.csdn.net/weixin_49640089/article/details/114770689
今日推荐