关闭

二叉搜索树与双向链表

标签: 二叉树搜索
172人阅读 评论(0) 收藏 举报
分类:




BinaryTreeNode* Convert(BinaryTreeNode*pRootOfTree)
{
BinaryTreeNode* pLastNodeInList = NULL;
ConvertNode(pRootOfTree, &pLastNodeInList);
//pLasstNodeInList指向双向链表的尾结点,我们需要返回头结点
BinaryTreeNode* pHeadOfList = pLastNodeInList;
while (pHeadOfList != NULL&&pHeadOfList->m_pLeft != NULL)
pHeadOfList = pHeadOfList->m_pLeft;
return pHeadOfList;
}
void ConvertNode(BinaryTreeNode* pNode, BinaryTreeNode** pLastNodeInList)
{
if (pNode == NULL)
return;
BinaryTreeNode *pCurrent = pNode;
if (pCurrent->m_pLeft != NULL)
ConvertNode(pCurrent->m_pLeft, pLastNodeInList);
pCurrent->m_pLeft = *pLastNodeInList;
if (*pLastNodeInList != NULL)
(*pLastNodeInList)->m_pRight = pCurrent;
*pLastNodeInList = pCurrent;
if (pCurrent->m_pRight != NULL)
ConvertNode(pCurrent->m_pRight, pLastNodeInList);
}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:167087次
    • 积分:6254
    • 等级:
    • 排名:第4060名
    • 原创:479篇
    • 转载:2篇
    • 译文:1篇
    • 评论:3条
    最新评论