二叉搜索树转化为有序双向链表

来源:互联网 发布:顶级域名绑定 编辑:程序博客网 时间:2024/04/28 12:41
#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);}

0 0
原创粉丝点击