二叉树的最近公共祖先、两个最远节点、第K层结点个数、出现次数超过一半的元素

1.个结点的最近公共祖先
实现思路:保存两个节点的路径,然后进行判断相同根节点

typedef BNode TreeNode;

bool GetNodePath(TreeNode* pHead, TreeNode* pNode, std::list<TreeNode*>&list)
{
    //没有找到,就在就直接pop
    if (pHead == pNode)
    {
        return true;
    }

    list.push_back(pHead);
    bool found = false;


    //前序遍历
    if (pHead->pLeft)
    {
        found = GetNodePath(pHead->pLeft, pNode, list);
    }

    if (!found&&pHead->pRight)
    {
        found = GetNodePath(pHead->pRight, pNode, list);
    }

    if (!found)
    {
        list.pop_back();
    }
    return found;

}

//求最近公共祖先
TreeNode* GetLastCommon
   (
    const std::list<TreeNode*>&path1,
    const std::list<TreeNode*>&path2
    )
{
    std::list<TreeNode*>::const_iterator it1 = path1.begin();
    std::list<TreeNode*>::const_iterator it2 = path2.begin();

    TreeNode* pLast = NULL;
    while (it1 != path1.end() && it2 != path2.end())
    {
        if (*it1 == *it2)
        {
            pLast = *it1;
        }
        else
        {
            break;
        }
        it1++;
        it2++;
    }
    return pLast;

}


TreeNode* LastCommonParent(TreeNode*pHead, TreeNode* pNode1, TreeNode* pNode2)
{
    if (pHead == NULL || pNode1 == NULL || pNode2 == NULL)
    {
        return NULL;
    }

    //获取两条路径
    std::list<TreeNode*>path1;
    GetNodePath(pHead,pNode1,path1);

    std::list<TreeNode*>path2;
    GetNodePath(pHead, pNode2, path2);


    return GetLastCommon(path1,path2);
}

2.最远两个结点的距离
实现思路:两个结点的最远距离,就是左右子树的高度之和,求最大值
从下往上求,后序遍历


size_t GetFarthestDistance(TreeNode* pHead,size_t&maxLength)
{
    if (pHead == NULL)
    {
        return 0;
    }
    size_t left=GetFarthestDistance(pHead->pLeft,maxLength);
    size_t right=GetFarthestDistance(pHead->pRight,maxLength);

    if (left + right > maxLength)
    {
        maxLength = left + right;
    }

    return left > right ? left + 1 : right + 1;
}

3.叶子节点的个数
思路:遍历一次树,当左右孩子均为空,说明就是叶子节点,进行计数

void _GetLeafCount(BNode*  pRoot,size_t&count)
{
    if (pRoot)
    {
        if (!(pRoot->pLeft) && !(pRoot->pRight))
        {
            count++;
        }
        _GetLeafCount(pRoot->pLeft, count);
        _GetLeafCount(pRoot->pRight, count);
    }

}

4.第K 层节点的个数

void _GetKthLeafCount(BNode*  pRoot,size_t k,size_t K, size_t&count)
{
    if (pRoot)
    {
        if (k==K)
        {
            count++;
        }
        k++;
        _GetKthLeafCount(pRoot->pLeft,k,K ,count);   //注意此处是个坑,一次递归中不能++ 两次
        _GetKthLeafCount(pRoot->pRight,k,K, count);
    }
}

5.一个数组中有一个数字的次数超过了数组的一半,求出这个字符。如:int a[]={2,3,2,2,2,2,2,5,4,1,2,3},求出超过一半的数字是2。
//思路1:用一个计数器,一个保存值,如果相等,则进行++,不相等进行–,等于-1时,进行更新改值
//思路2:用快速排序排序,中间元素即是该元素

size_t GetHalfTimesNum1(int* arr,size_t size)
{
    if (arr == NULL || size < 1)
    {
        return 0;
    }
    int num = arr[0];
    int count = 0;

    for (int i = 0; i < size; i++)
    {
        if (num == arr[i])
        {
            count++;
        }
        else
        {
            count--;
        }


        if (count < 0)
        {
            num = arr[i];
        }
    }


    return num;

}
size_t GetHalfTimesNum2(int* arr, size_t size)
{
    sort(arr, arr + size);
    return arr[size/2];
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值