鍘熸枃鍦板潃錛?a >http://zhedahht.blog.163.com/blog/static/254111742007127104759245/
棰樼洰錛氳緭鍏ヤ竴媯典簩鍏冩煡鎵炬爲錛屽皢璇ヤ簩鍏冩煡鎵炬爲杞崲鎴愪竴涓帓搴忕殑鍙屽悜閾捐〃銆傝姹備笉鑳藉垱寤轟換浣曟柊鐨勭粨鐐癸紝鍙皟鏁存寚閽堢殑鎸囧悜銆?/span>
銆銆姣斿灝嗕簩鍏冩煡鎵炬爲
10
/ \
6 14
/ \ /銆 \
銆4 8 12 銆 16
杞崲鎴愬弻鍚戦摼琛?/span>
4=6=8=10=12=14=16銆?/span>
銆銆鍒嗘瀽錛氭湰棰樻槸寰蔣鐨勯潰璇曢銆傚緢澶氫笌鏍戠浉鍏崇殑棰樼洰閮芥槸鐢ㄩ掑綊鐨勬濊礬鏉ヨВ鍐籌紝鏈涔熶笉渚嬪銆備笅闈㈡垜浠敤涓ょ涓嶅悓鐨勯掑綊鎬濊礬鏉ュ垎鏋愩?/span>
銆銆鎬濊礬涓錛氬綋鎴戜滑鍒拌揪鏌愪竴緇撶偣鍑嗗璋冩暣浠ヨ緇撶偣涓烘牴緇撶偣鐨勫瓙鏍戞椂錛屽厛璋冩暣鍏跺乏瀛愭爲灝嗗乏瀛愭爲杞崲鎴愪竴涓帓濂藉簭鐨勫乏瀛愰摼琛紝鍐嶈皟鏁村叾鍙沖瓙鏍戣漿鎹㈠彸瀛愰摼琛ㄣ傛渶榪戦摼鎺ュ乏瀛愰摼琛ㄧ殑鏈鍙崇粨鐐癸紙宸﹀瓙鏍戠殑鏈澶х粨鐐癸級銆佸綋鍓嶇粨鐐瑰拰鍙沖瓙閾捐〃鐨勬渶宸︾粨鐐癸紙鍙沖瓙鏍戠殑鏈灝忕粨鐐癸級銆備粠鏍戠殑鏍圭粨鐐瑰紑濮嬮掑綊璋冩暣鎵鏈夌粨鐐廣?/span>
銆銆鎬濊礬浜岋細鎴戜滑鍙互涓簭閬嶅巻鏁存5鏍戙傛寜鐓ц繖涓柟寮忛亶鍘嗘爲錛屾瘮杈冨皬鐨勭粨鐐瑰厛璁塊棶銆傚鏋滄垜浠瘡璁塊棶涓涓粨鐐癸紝鍋囪涔嬪墠璁塊棶榪囩殑緇撶偣宸茬粡璋冩暣鎴愪竴涓帓搴忓弻鍚戦摼琛紝鎴戜滑鍐嶆妸璋冩暣褰撳墠緇撶偣鐨勬寚閽堝皢鍏墮摼鎺ュ埌閾捐〃鐨勬湯灝俱傚綋鎵鏈夌粨鐐歸兘璁塊棶榪囦箣鍚庯紝鏁存5鏍戜篃灝辮漿鎹㈡垚涓涓帓搴忓弻鍚戦摼琛ㄤ簡銆?/span>
鍙傝冧唬鐮侊細
棣栧厛鎴戜滑瀹氫箟浜屽厓鏌ユ壘鏍戠粨鐐圭殑鏁版嵁緇撴瀯濡備笅錛?br style="LINE-HEIGHT: 22px"> struct BSTreeNode // a node in the binary search tree
{
int m_nValue; // value of node
BSTreeNode *m_pLeft; // left child of node
BSTreeNode *m_pRight; // right child of node
};
鎬濊礬涓瀵瑰簲鐨勪唬鐮侊細
///////////////////////////////////////////////////////////////////////
// Covert a sub binary-search-tree into a sorted double-linked list
// Input: pNode - the head of the sub tree
// asRight - whether pNode is the right child of its parent
// Output: if asRight is true, return the least node in the sub-tree
// else return the greatest node in the sub-tree
///////////////////////////////////////////////////////////////////////
BSTreeNode* ConvertNode(BSTreeNode* pNode, bool asRight)
{
if(!pNode)
return NULL;
BSTreeNode *pLeft = NULL;
BSTreeNode *pRight = NULL;
// Convert the left sub-tree
if(pNode->m_pLeft)
pLeft = ConvertNode(pNode->m_pLeft, false);
// Connect the greatest node in the left sub-tree to the current node
if(pLeft)
{
pLeft->m_pRight = pNode;
pNode->m_pLeft = pLeft;
}
// Convert the right sub-tree
if(pNode->m_pRight)
pRight = ConvertNode(pNode->m_pRight, true);
// Connect the least node in the right sub-tree to the current node
if(pRight)
{
pNode->m_pRight = pRight;
pRight->m_pLeft = pNode;
}
BSTreeNode *pTemp = pNode;
// If the current node is the right child of its parent,
// return the least node in the tree whose root is the current node
if(asRight)
{
while(pTemp->m_pLeft)
pTemp = pTemp->m_pLeft;
}
// If the current node is the left child of its parent,
// return the greatest node in the tree whose root is the current node
else
{
while(pTemp->m_pRight)
pTemp = pTemp->m_pRight;
}
return pTemp;
}
///////////////////////////////////////////////////////////////////////
// Covert a binary search tree into a sorted double-linked list
// Input: the head of tree
// Output: the head of sorted double-linked list
///////////////////////////////////////////////////////////////////////
BSTreeNode* Convert(BSTreeNode* pHeadOfTree)
{
// As we want to return the head of the sorted double-linked list,
// we set the second parameter to be true
return ConvertNode(pHeadOfTree, true);
}
鎬濊礬浜屽搴旂殑浠g爜錛?br style="LINE-HEIGHT: 22px">///////////////////////////////////////////////////////////////////////
// Covert a sub binary-search-tree into a sorted double-linked list
// Input: pNode - the head of the sub tree
// pLastNodeInList - the tail of the double-linked list
///////////////////////////////////////////////////////////////////////
void ConvertNode(BSTreeNode* pNode, BSTreeNode*& pLastNodeInList)
{
if(pNode == NULL)
return;
BSTreeNode *pCurrent = pNode;
// Convert the left sub-tree
if (pCurrent->m_pLeft != NULL)
ConvertNode(pCurrent->m_pLeft, pLastNodeInList);
// Put the current node into the double-linked list
pCurrent->m_pLeft = pLastNodeInList;
if(pLastNodeInList != NULL)
pLastNodeInList->m_pRight = pCurrent;
pLastNodeInList = pCurrent;
// Convert the right sub-tree
if (pCurrent->m_pRight != NULL)
ConvertNode(pCurrent->m_pRight, pLastNodeInList);
}
///////////////////////////////////////////////////////////////////////
// Covert a binary search tree into a sorted double-linked list
// Input: pHeadOfTree - the head of tree
// Output: the head of sorted double-linked list
///////////////////////////////////////////////////////////////////////
BSTreeNode* Convert_Solution1(BSTreeNode* pHeadOfTree)
{
BSTreeNode *pLastNodeInList = NULL;
ConvertNode(pHeadOfTree, pLastNodeInList);
// Get the head of the double-linked list
BSTreeNode *pHeadOfList = pLastNodeInList;
while(pHeadOfList && pHeadOfList->m_pLeft)
pHeadOfList = pHeadOfList->m_pLeft;
return pHeadOfList;
}