微软编程题-把二元查找树变成排序的双向链表

时间:2022-10-23 14:46:13

题目:输入一棵二元查找树,将该二元查找树转换成一个排序的双向链表。要求不能创建任何新的节点,只调整指针的指向。

微软编程题-把二元查找树变成排序的双向链表

转换成 4=6=8=10=12=14=16

#include <stdio.h>
#include <iostream>
using namespace std;

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

typedef BSTreeNode DoubleList;//定义双向链表类型
DoubleList *pHead;//双向链表头结点
DoubleList *pListIndex;//指向链表上一个节点

void convertToDoubleList(BSTreeNode *pCurrent);
void CreateBSTreeNode(BSTreeNode* &pCurrent,int value)//由于该函数会改变指针值,所以用引用传递
{
    if(pCurrent==NULL)//如果该节点为空,则创建一个新的节点
    {
        BSTreeNode* pBSTree=new BSTreeNode();//分配一个新节点
        pBSTree->m_pLeft=NULL;
        pBSTree->m_pRight=NULL;
        pBSTree->m_nValue=value;
        pCurrent=pBSTree;//将分配的新节点赋给当前节点
    }
    else
    {
        if((pCurrent->m_nValue)>value)
        {
            CreateBSTreeNode(pCurrent->m_pLeft,value);
        }
        else if((pCurrent->m_nValue)<value)
        {
            CreateBSTreeNode(pCurrent->m_pRight,value);
        }
        else
        {
            cout<<"重复加入节点"<<endl;
        }
    }
}
//中序遍历二叉树
void ergodicBSTree(BSTreeNode *pCurrent)
{
    if(pCurrent==NULL)
    {
        return;
    }
    if(pCurrent->m_pLeft!=NULL)
    {
        ergodicBSTree(pCurrent->m_pLeft);
    }
    convertToDoubleList(pCurrent);
    if(pCurrent->m_pRight!=NULL)
    {
        ergodicBSTree(pCurrent->m_pRight);
    }
}

void convertToDoubleList(BSTreeNode *pCurrent)
{
    pCurrent->m_pLeft=pListIndex;
    if(pListIndex!=NULL)
    {
        pListIndex->m_pRight=pCurrent;
    }
    else
    {
        pHead=pCurrent;
    }
    pListIndex=pCurrent;
    cout<<pCurrent->m_nValue<<endl;
}
//方法2:
/* 递归遍历中的转换过程 参数:处理当前结点,当前链表的最后一个结点(初始值为空) */  
void ConvertNode(BinaryTreeNode* pNode, BinaryTreeNode** pLastNodeInList)  
{  
    if(pNode == NULL)  
        return;  
    BinaryTreeNode* pCurrent = pNode;  
    //递归处理左子树 
    if (pCurrent->m_pLeft != NULL)  
        ConvertNode(pNode->m_pLeft,pLastNodeInList);  
    //处理当前结点 
    pCurrent->m_pLeft = *pLastNodeInList;    //将当前结点的左指针指向已经转换好的链表的最后一个位置 
    if (*pLastNodeInList!=NULL)  
        *pLastNodeInList->m_pRight = pCurrent;//将已转换好的链表的最后一个结点的右指针指向当前结点 

    *pLastNodeInList = pCurrent;//更新链表的最后一个结点 
    //递归处理当前结点的右子树 
    if (pCurrent->m_pRight != NULL)  
        ConvertNode(pNode->m_pRight, pLastNodeInList);  
}  

BinaryTreeNode* Convert(BinaryTreeNode* pRootInTree)  
{  
    BinaryTreeNode* pLastNodeInList = NULL;  

    ConvertNode(pRootInTree, &pLastNodeInList);  

    //pLastNodeInList指向双向链表的尾结点,再次遍历找到头结点 
    BinaryTreeNode* pHeadOfList = pLastNodeInList;  
    while(pHeadOfList != NULL && pHeadOfList->m_pLeft != NULL)  
        pHeadOfList = pHeadOfList->m_pLeft;  

    return pHeadOfList;  
} 



int main()
{
    BSTreeNode *pRoot=NULL;
    pListIndex=NULL;
    pHead=NULL;
    CreateBSTreeNode(pRoot,10);
    CreateBSTreeNode(pRoot,4);
    CreateBSTreeNode(pRoot,6);
    CreateBSTreeNode(pRoot,8);
    CreateBSTreeNode(pRoot,12);
    CreateBSTreeNode(pRoot,14);
    CreateBSTreeNode(pRoot,16);
    ergodicBSTree(pRoot);
    system("pause");
    return 0;
}

微软编程题-把二元查找树变成排序的双向链表