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

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

这里写图片描述

转换成 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;
}

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值