《剑指offer》面试题36:将二叉搜索树转换成双向链表

更多剑指offer面试习题请点击:《剑指offer》(第二版)题集目录索引

关于二叉树的增删查改:二叉搜索树的基本操作


一、题目

  输入一棵二叉搜索树,将该二叉搜索树转换成一个排序的双向链表。要求不能创建任何新的结点,只能调整树中结点指针的指向。二叉树节点定义如下:

struct BinaryTreeNode
{
    int             m_nValue;
    BinaryTreeNode* m_pLeft;
    BinaryTreeNode* m_pRight;
};

这里写图片描述


二、解题思路

  1. 要使求链表是有序的,可以借助二叉树中序遍历,因为二叉搜索树的中序遍历访问节点的顺序使从小到大。当遍历访问到根结点时,假设根结点的左侧已经处理好,只需将根结点的m_pLeft指向上次访问的最近结点(左子树中最大值结点),进而更新当前链表的最后一个结点指针。

  2. 由于中序遍历过程和转换成链表的过程是一样的,直接采用递归处理。
    这里写图片描述


三、实现代码

BinaryTreeNode* Convert(BinaryTreeNode* pRootOfTree)
{
    // 记录链表的尾节点
    BinaryTreeNode* pLastNodeInList = nullptr; 

    // 把二叉树转换成双向链表
    ConvertNode(pRootOfTree, &pLastNodeInList);

    // 找到双向链表的头结点
    BinaryTreeNode* pHeadOfList = pLastNodeInList; 
    while (pHeadOfList != nullptr && pHeadOfList->m_pLeft != nullptr)
    {
        pHeadOfList = pHeadOfList->m_pLeft;
    }

    // 返回双向链表的头结点
    return pHeadOfList;
}

void ConvertNode(BinaryTreeNode* pNode, BinaryTreeNode** pLastNodeInList)
{
    if (pNode == nullptr)
        return;

    BinaryTreeNode* pCurrent = pNode; 

    // 往左子树递归, 从最左节点开始链起
    if (pCurrent->m_pLeft != nullptr)
    {
        ConvertNode(pCurrent->m_pLeft, pLastNodeInList);
    }

    // 处理当前节点
    //将当前节点的左指针指向已经转换好的链表的尾节点
    pCurrent->m_pLeft = *pLastNodeInList;

    //将已经转换好的链表的尾节点的右指针指向当前节点
    if (*pLastNodeInList != nullptr)
    {
        (*pLastNodeInList)->m_pRight = pCurrent;
    }

    //更新已经转换好的链表的尾节点
    *pLastNodeInList = pCurrent;

    // 递归去处理右子树
    if (pCurrent->m_pRight != nullptr)
    {
        ConvertNode(pCurrent->m_pRight, pLastNodeInList);
    }
}

四、测试代码

void Test(char* testName, BinaryTreeNode* pRootOfTree)
{
    if (testName != nullptr)
        printf("%s begins:\n", testName);

    PrintTree(pRootOfTree);

    BinaryTreeNode* pHeadOfList = Convert(pRootOfTree);

    PrintDoubleLinkedList(pHeadOfList);
    printf("\n================================================================================\n\n");
}

//            10
//         /      \
//        6        14
//       /\        /\
//      4  8     12  16
void Test1()
{
    BinaryTreeNode* pNode10 = CreateBinaryTreeNode(10);
    BinaryTreeNode* pNode6 = CreateBinaryTreeNode(6);
    BinaryTreeNode* pNode14 = CreateBinaryTreeNode(14);
    BinaryTreeNode* pNode4 = CreateBinaryTreeNode(4);
    BinaryTreeNode* pNode8 = CreateBinaryTreeNode(8);
    BinaryTreeNode* pNode12 = CreateBinaryTreeNode(12);
    BinaryTreeNode* pNode16 = CreateBinaryTreeNode(16);

    ConnectTreeNodes(pNode10, pNode6, pNode14);
    ConnectTreeNodes(pNode6, pNode4, pNode8);
    ConnectTreeNodes(pNode14, pNode12, pNode16);

    Test("Test1", pNode10);

    DestroyList(pNode4);
}

//               5
//              /
//             4
//            /
//           3
//          /
//         2
//        /
//       1
void Test2()
{
    BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5);
    BinaryTreeNode* pNode4 = CreateBinaryTreeNode(4);
    BinaryTreeNode* pNode3 = CreateBinaryTreeNode(3);
    BinaryTreeNode* pNode2 = CreateBinaryTreeNode(2);
    BinaryTreeNode* pNode1 = CreateBinaryTreeNode(1);

    ConnectTreeNodes(pNode5, pNode4, nullptr);
    ConnectTreeNodes(pNode4, pNode3, nullptr);
    ConnectTreeNodes(pNode3, pNode2, nullptr);
    ConnectTreeNodes(pNode2, pNode1, nullptr);

    Test("Test2", pNode5);

    DestroyList(pNode1);
}

// 1
//  \
//   2
//    \
//     3
//      \
//       4
//        \
//         5
void Test3()
{
    BinaryTreeNode* pNode1 = CreateBinaryTreeNode(1);
    BinaryTreeNode* pNode2 = CreateBinaryTreeNode(2);
    BinaryTreeNode* pNode3 = CreateBinaryTreeNode(3);
    BinaryTreeNode* pNode4 = CreateBinaryTreeNode(4);
    BinaryTreeNode* pNode5 = CreateBinaryTreeNode(5);

    ConnectTreeNodes(pNode1, nullptr, pNode2);
    ConnectTreeNodes(pNode2, nullptr, pNode3);
    ConnectTreeNodes(pNode3, nullptr, pNode4);
    ConnectTreeNodes(pNode4, nullptr, pNode5);

    Test("Test3", pNode1);

    DestroyList(pNode1);
}

// 树中只有1个结点
void Test4()
{
    BinaryTreeNode* pNode1 = CreateBinaryTreeNode(1);
    Test("Test4", pNode1);

    DestroyList(pNode1);
}

// 树中没有结点
void Test5()
{
    Test("Test5", nullptr);
}

int main(int argc, char* argv[])
{
    Test1();
    Test2();
    Test3();
    Test4();
    Test5();

    system("pause");
    return 0;
}

五、测试结果

这里写图片描述


六、基础代码

1. 打印双向链表

void PrintDoubleLinkedList(BinaryTreeNode* pHeadOfList)
{
    BinaryTreeNode* pNode = pHeadOfList;

    printf("\nThe nodes from left to right are:\n");
    while (pNode != nullptr)
    {
        printf("%d\t", pNode->m_nValue);

        if (pNode->m_pRight == nullptr)
            break;
        pNode = pNode->m_pRight;
    }

    printf("\nThe nodes from right to left are:\n");
    while (pNode != nullptr)
    {
        printf("%d\t", pNode->m_nValue);

        if (pNode->m_pLeft == nullptr)
            break;
        pNode = pNode->m_pLeft;
    }

    printf("\n");
}

2. 销毁双向链表

void DestroyList(BinaryTreeNode* pHeadOfList)
{
    BinaryTreeNode* pNode = pHeadOfList;
    while (pNode != nullptr)
    {
        BinaryTreeNode* pNext = pNode->m_pRight;

        delete pNode;
        pNode = pNext;
    }
}

3. 创建二叉树节点

BinaryTreeNode* CreateBinaryTreeNode(int value)
{
    BinaryTreeNode* NewNode = new BinaryTreeNode;
    NewNode->m_nValue = value;
    NewNode->m_pLeft  = nullptr;
    NewNode->m_pRight = nullptr;

    return NewNode;
}

4. 链接二叉树

void ConnectTreeNodes(BinaryTreeNode *root, BinaryTreeNode *LeftChild, BinaryTreeNode *RightChild)
{
    assert(root != nullptr);

    root->m_pLeft = LeftChild;
    root->m_pRight = RightChild;
}

5. 按中序遍历打印二叉树

void PrintTree(BinaryTreeNode* pNode)
{
    if (pNode == nullptr)
    {
        return;
    }

    PrintTree(pNode->m_pLeft);
    printf("%d\t", pNode->m_nValue);
    PrintTree(pNode->m_pRight);
}

猜你喜欢

转载自blog.csdn.net/Tianzez/article/details/80383051