二叉搜索树 转 有序双向链表

/* 27:> 二叉搜索树 转 有序双向链表 左孩子指向前驱,右孩子指向后继 递归,动态规划 */
BinTree* First(BinTree* pRoot)
{
    if (pRoot == NULL)
        return NULL;

    BinTree* pF = pRoot;
    while (pF->m_pLeft)
    {
        pF = pF->m_pLeft;
    }
    return pF;
}
BinTree* Last(BinTree* pRoot)
{
    if (pRoot == NULL)
        return NULL;

    BinTree* pL = pRoot;
    while (pL->m_pRight)
    {
        pL = pL->m_pRight;
    }
    return pL;
}

void Convert(BinTree*& pRoot)
{
    if (pRoot == NULL)
        return;

    BinTree*pL = pRoot->m_pLeft;
    BinTree*pR = pRoot->m_pRight;

    if (pL && pL->m_pRight != pRoot)
    {
        pRoot->m_pLeft = Last(pL);
        pRoot->m_pLeft->m_pRight = pRoot;
        Convert(pL);
    }

    if (pR && pR->m_pLeft != pRoot)
    {
        pRoot->m_pRight = First(pR);
        pRoot->m_pRight->m_pLeft = pRoot;
        Convert(pR);
    }
}

你可能感兴趣的:(二叉搜索树 转 有序双向链表)