二叉搜索树与双向链表




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);
}

阅读更多
文章标签: 二叉树 搜索
个人分类: 剑指offer
上一篇复杂链表的复制
下一篇28.字符串的排列
博主设置当前文章不允许评论。

没有更多推荐了,返回首页

关闭
关闭