#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);
}
二叉搜索樹轉化爲有序雙向鏈表
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.