【面试题】把二元查找树转变成排序的双向链表
题目:输入一棵二元查找树,将该二元查找树转换成一个排序的双向链表。要求不能创建任何新的结点,只调整指针的指向。
比如将二元查找树/* 建立二叉排序树 */void addBSTreeNode(BSTreeNode *&pCurrent,int value)//在这个函数中会要改变指针值,一定要记得使用引用传递 { if (pCurrent==NULL) { BSTreeNode* pBSTree=new BSTreeNode(); pBSTree->m_nValue=value; pBSTree->m_pLeft=NULL; pBSTree->m_pRight=NULL; pCurrent=pBSTree; } else if (pCurrent->m_nValue<value) { addBSTreeNode(pCurrent->m_pRight,value); } else if (pCurrent->m_nValue>value) { addBSTreeNode(pCurrent->m_pLeft,value); } else { cout<<"重复节点"<<endl; } }/*将一颗二叉搜索树转换成一个双向链表*///pNode:当前节点,即子二叉树的根节点//pLastNodeInList:双向链表中的最后一个节点void convertToDoubleList(BSTreeNode* pNode, BSTreeNode*& pLastNodeInList){ if(pNode == NULL) return; BSTreeNode *pCurrent = pNode; /*转换左子树*/ if (pCurrent->m_pLeft != NULL) convertToDoubleList(pCurrent->m_pLeft, pLastNodeInList); // 把当前节点放入双向链表中 pCurrent->m_pLeft = pLastNodeInList;//设置当前节点的左指针 if(pLastNodeInList != NULL) pLastNodeInList->m_pRight = pCurrent; pLastNodeInList=pCurrent;// 转换右子树 if (pCurrent->m_pRight != NULL) convertToDoubleList(pCurrent->m_pRight, pLastNodeInList);}//BSTreeNode* Convert(BSTreeNode* pHeadOfTree){ BSTreeNode *pLastNodeInList = NULL;//双链表最后一个节点 convertToDoubleList(pHeadOfTree, pLastNodeInList); // 获取双向链表的头节点 BSTreeNode *pHeadOfList = pLastNodeInList; while(pHeadOfList && pHeadOfList->m_pLeft) pHeadOfList = pHeadOfList->m_pLeft; return pHeadOfList; //返回双向链表的头节点}int main(){/****创建二叉查找树****/ BSTreeNode *pRoot=NULL; addBSTreeNode(pRoot,10); addBSTreeNode(pRoot,6); addBSTreeNode(pRoot,14); addBSTreeNode(pRoot,4); addBSTreeNode(pRoot,8); addBSTreeNode(pRoot,12); addBSTreeNode(pRoot,16); /***************转换为双向链表*******************/ BSTreeNode *pHeadOfList=Convert(pRoot);/**********************输出双向链表******************/ while(pHeadOfList!=NULL) { cout<<pHeadOfList->m_nValue<<"->"; pHeadOfList=pHeadOfList->m_pRight; } return 0;}