寻找二叉树中任意两个数的公共祖先

该博客介绍了如何在二叉搜索树中寻找两个节点的最近公共祖先。通过遍历树节点并比较目标节点的索引位置,实现找到公共祖先。代码示例使用了C++,主要通过比较节点索引来确定公共祖先的位置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

代码如下:

void  Anales(int arr[],int p, int q)

{

    int pos = -1;

    int size = 0;

    int as[1] = { 0 };

    int indexP = 0;

    int indexQ = 0;

    while (arr[size] != as[1])

    {

        if (arr[size] == p)

        {

            indexP = size;

        }

        if (arr[size] == q)

        {

            indexQ = size;

        }

        size++;

    }

    if (indexP > indexQ)

    {

        int subInt = indexP - indexQ; //如索引的间隔正好等于更小的那个索引,那么它们的公//共祖先索引为0

        if (subInt == indexQ)

        {

            pos = 0;

        }

        else 

        {

            int fatherP = indexP;

            while (fatherP > indexQ)

            {

                if (fatherP % 2) //如果当前索引为偶数,它的上一级 = (当前索引 - 2)/2

                {

                    fatherP = (fatherP - 2) / 2;

                }

                else //如果当前索引为奇数,它的上一级 = (当前索引 - 1)/2

                {

                    fatherP = (fatherP - 1) / 2;

                }

            }

            if (fatherP == indexQ) 

            {

                pos = fatherP;

            }

            else 

            {

                //同时寻找上级直到相等   

                while (fatherP != indexQ) 

                {

                    if (fatherP % 2)

                    {

                        fatherP = (fatherP - 2) / 2;

                    }

                    else 

                    {

                        fatherP = (fatherP - 1) / 2;

                    }

                    if (indexQ % 2)

                    {

                        indexQ = (indexQ - 2) / 2;

                    }

                    else 

                    {

                        indexQ = (indexQ - 1) / 2;

                    }

                }

                pos = indexQ;

            }

    

        }

    }

    else if (indexQ > indexP)

    {

        int subInt = indexQ - indexP;

        if (subInt == indexP)

        {

            pos = 0;

        }

        else

        {

            while (indexQ > indexP)

            {

                if (indexQ % 2)

                {

                    indexQ = (indexQ - 2) / 2;

                }

                else

                {

                    indexQ = (indexQ - 1) / 2;

                }

            }

            if (indexP == indexQ)

            {

                pos = indexQ;

            }

            else

            {

                while (indexP != indexQ)

                {

                    if (indexP % 2)

                    {

                        indexP = (indexP - 2) / 2;

                    }

                    else

                    {

                        indexP = (indexP - 1) / 2;

                    }

                    if (indexQ % 2)

                    {

                        indexQ = (indexQ - 2) / 2;

                    }

                    else

                    {

                        indexQ = (indexQ - 1) / 2;

                    }

                }

                pos = indexQ;

            }

        }

    }

    else 

    {

        pos = indexP;

    }

    

    std::cout << "公共祖先:" << arr[pos] << "\t 索引:" << pos << std::endl;

}

   

int main()

{

int arr[15] = { 3,5,1,6,2,0,8,0,0,7,4,0,0,0,0 };

    Anales(arr,5,4);

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值