栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

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

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

代码如下:

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);

}

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/960510.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号