日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

程序员面试题精选100题(01)-把二元查找树转变成排序的双向链表[数据结构]

發(fā)布時間:2025/3/21 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 程序员面试题精选100题(01)-把二元查找树转变成排序的双向链表[数据结构] 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
題目:輸入一棵二元查找樹,將該二元查找樹轉(zhuǎn)換成一個排序的雙向鏈表。要求不能創(chuàng)建任何新的結(jié)點,只調(diào)整指針的指向。

  比如將二元查找樹
??? ??????????????????????????????????????? 10
????????????????????????????????????????? /??? \
??????????????????????????????????????? 6?????? 14
????????????????????????????????????? /? \???? /  \
??????????????????????????????????  4???? 8? 12  ? 16
轉(zhuǎn)換成雙向鏈表

4=6=8=10=12=14=16。

  分析:本題是微軟的面試題。很多與樹相關(guān)的題目都是用遞歸的思路來解決,本題也不例外。下面我們用兩種不同的遞歸思路來分析。

  思路一:當(dāng)我們到達(dá)某一結(jié)點準(zhǔn)備調(diào)整以該結(jié)點為根結(jié)點的子樹時,先調(diào)整其左子樹將左子樹轉(zhuǎn)換成一個排好序的左子鏈表,再調(diào)整其右子樹轉(zhuǎn)換右子鏈表。最近鏈接左子鏈表的最右結(jié)點(左子樹的最大結(jié)點)、當(dāng)前結(jié)點和右子鏈表的最左結(jié)點(右子樹的最小結(jié)點)。從樹的根結(jié)點開始遞歸調(diào)整所有結(jié)點。

  思路二:我們可以中序遍歷整棵樹。按照這個方式遍歷樹,比較小的結(jié)點先訪問。如果我們每訪問一個結(jié)點,假設(shè)之前訪問過的結(jié)點已經(jīng)調(diào)整成一個排序雙向鏈表,我們再把調(diào)整當(dāng)前結(jié)點的指針將其鏈接到鏈表的末尾。當(dāng)所有結(jié)點都訪問過之后,整棵樹也就轉(zhuǎn)換成一個排序雙向鏈表了。

參考代碼:

首先我們定義二元查找樹結(jié)點的數(shù)據(jù)結(jié)構(gòu)如下:
???

struct BSTreeNode // a node in the binary search tree{int m_nValue; // value of nodeBSTreeNode *m_pLeft; // left child of nodeBSTreeNode *m_pRight; // right child of node};

思路一對應(yīng)的代碼:

/// // 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-treeif(pNode->m_pLeft)pLeft = ConvertNode(pNode->m_pLeft, false);// Connect the greatest node in the left sub-tree to the current nodeif(pLeft){pLeft->m_pRight = pNode;pNode->m_pLeft = pLeft;}// Convert the right sub-treeif(pNode->m_pRight)pRight = ConvertNode(pNode->m_pRight, true);// Connect the least node in the right sub-tree to the current nodeif(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 nodeif(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 nodeelse{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 truereturn ConvertNode(pHeadOfTree, true); }


思路二對應(yīng)的代碼:

/// // 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-treeif (pCurrent->m_pLeft != NULL)ConvertNode(pCurrent->m_pLeft, pLastNodeInList);// Put the current node into the double-linked listpCurrent->m_pLeft = pLastNodeInList; if(pLastNodeInList != NULL)pLastNodeInList->m_pRight = pCurrent;pLastNodeInList = pCurrent;// Convert the right sub-treeif (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 listBSTreeNode *pHeadOfList = pLastNodeInList;while(pHeadOfList && pHeadOfList->m_pLeft)pHeadOfList = pHeadOfList->m_pLeft;return pHeadOfList; }


本文已經(jīng)收錄到《劍指Offer——名企面試官精講典型編程題》一書中,有改動,歡迎關(guān)注。

博主何海濤對本博客文章享有版權(quán)。網(wǎng)絡(luò)轉(zhuǎn)載請注明出處http://zhedahht.blog.163.com/。整理出版物請和作者聯(lián)系。對解題思路有任何建議,歡迎在評論中告知,或者加我微博http://weibo.com/zhedahht或者http://t.163.com/zhedahht與我討論。謝謝。我還寫了這篇博客的英文版,感興趣的讀者請到http://codercareer.blogspot.com/2011/09/interview-question-no-1-binary-search.html查看。謝謝。

總結(jié)

以上是生活随笔為你收集整理的程序员面试题精选100题(01)-把二元查找树转变成排序的双向链表[数据结构]的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。