#include<iostream>
using namespace std;
struct BinaryTreeNode{
int m_value;
BinaryTreeNode* m_left;
BinaryTreeNode* m_right;
};
BinaryTreeNode* Convert(BinaryTreeNode* pRootoftree){
BinaryTreeNode *pLastNodeInList = NULL;
ConvertNode(pRootoftree,&pLastNodeInList);
//pLastNodeList指向双向离岸边的尾节点
//我们需要返回头结点
BinaryTreeNode *pHeadoflist = pLastNodeInList;
while(pHeadoflist != NULL && pHeadoflist->m_left != NULL)
pHeadoflist = pHeadoflist->m_left;
return pHeadoflist;
}
void ConvertNode(BinaryTreeNode* pNode,BinaryTreeNode* pRootoftree** pLastNodeinlist){
if(pNode == NULL) return ;
BinaryTreeNode *pCurrent = pNode;
if(pCurrent->m_left != NULL)
ConvertNode(pCurrent->m_left,pLastNodeinlist);
pCurrent->m_left = *pLastNodeinlist;
if(*pLastNodeinlist != NULL)
(*pLastNodeinlist)->m_right = pCurrent;
*pLastNodeinlist = pCurrent;
if(pCurrent->m_right!=NULL)
ConvertNode(pCurrent->m_right,pLastNodeinlist);
}
二叉搜索树转化为有序双向链表
最新推荐文章于 2019-10-14 22:30:36 发布