青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

Where there is a dream ,there is hope

  C++博客 :: 首頁 :: 聯系 :: 聚合  :: 管理
  64 Posts :: 0 Stories :: 8 Comments :: 0 Trackbacks

常用鏈接

留言簿(1)

我參與的團隊

搜索

  •  

最新評論

閱讀排行榜

評論排行榜

原文地址:http://zhedahht.blog.163.com/blog/static/254111742007127104759245/
題目:輸入一棵二元查找樹,將該二元查找樹轉換成一個排序的雙向鏈表。要求不能創建任何新的結點,只調整指針的指向。

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

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

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

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

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

參考代碼:

首先我們定義二元查找樹結點的數據結構如下:
    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);
}

思路二對應的代碼:
///////////////////////////////////////////////////////////////////////
// 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;
}

posted on 2010-12-17 08:58 IT菜鳥 閱讀(343) 評論(0)  編輯 收藏 引用 所屬分類: 算法/數據結構
青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            亚洲人体1000| 欧美日韩一二三区| 91久久一区二区| 免费h精品视频在线播放| 久久精品国产第一区二区三区| 欧美一乱一性一交一视频| 欧美一区二视频| 久久亚洲不卡| 亚洲国产欧美精品| 一区二区三区精品| 欧美在线你懂的| 久久久之久亚州精品露出| 久久亚洲国产精品一区二区 | 欧美综合国产精品久久丁香| 欧美在线三区| 美女主播一区| 欧美三级网页| 黑人操亚洲美女惩罚| 亚洲麻豆国产自偷在线| 亚洲在线视频观看| 鲁大师成人一区二区三区 | 欧美伊人久久大香线蕉综合69| 久久精品在线免费观看| 亚洲第一福利社区| 午夜精品久久久久久久久久久| 久久亚洲色图| 国产伦理精品不卡| 亚洲日本在线观看| 久久爱www.| 亚洲免费观看高清完整版在线观看熊| 一区二区三区免费观看| 亚洲欧美日韩一区在线观看| 欧美国产激情二区三区| 亚洲欧美在线免费| 欧美日韩一区二区欧美激情 | 久久精品久久综合| 亚洲精品小视频在线观看| 欧美一区深夜视频| 国产精品sss| 亚洲国产日韩一级| 久久男女视频| 香蕉免费一区二区三区在线观看 | 久久精品视频在线看| 国产精品国产馆在线真实露脸| 亚洲日本在线观看| 免费的成人av| 久久精品99国产精品日本| 国产精品亚洲人在线观看| 宅男噜噜噜66国产日韩在线观看| 欧美国产日本| 免费人成精品欧美精品| 精品动漫一区二区| 久久亚洲一区二区| 欧美综合77777色婷婷| 国产亚洲综合性久久久影院| 午夜精品福利电影| 亚洲一区二区三区成人在线视频精品| 欧美精品国产精品日韩精品| 亚洲人午夜精品| 亚洲国产精品久久久久婷婷884 | 国产日韩欧美日韩大片| 性欧美1819sex性高清| 亚洲天堂黄色| 国产精品日韩欧美大师| 午夜天堂精品久久久久| 亚洲综合欧美日韩| 国产色视频一区| 久久久久久亚洲综合影院红桃| 欧美怡红院视频| 国产主播一区二区三区四区| 久久久久这里只有精品| 久久夜色精品国产| 亚洲美女黄网| 亚洲视频精选在线| 国产伦精品一区二区三区视频孕妇 | 亚洲一二三区精品| 国产亚洲毛片在线| 免费欧美网站| 欧美精品一区二区在线观看| 亚洲色图制服丝袜| 午夜在线视频一区二区区别| 精品动漫av| 亚洲人成绝费网站色www| 91久久在线| 欧美午夜电影在线| 久久久久网址| 欧美韩日精品| 欧美一区二区三区免费看| 久久本道综合色狠狠五月| 亚洲黑丝在线| 亚洲午夜精品网| 永久域名在线精品| 亚洲人成人一区二区在线观看| 国产精品mm| 蘑菇福利视频一区播放| 欧美视频专区一二在线观看| 久久久久久久网站| 欧美日韩国产美| 久久久另类综合| 欧美日韩一区二区三区免费| 久久天堂成人| 国产精品成人国产乱一区| 久久夜色精品国产亚洲aⅴ| 欧美日韩国产不卡| 免费看av成人| 国产欧美一区二区三区视频| 亚洲国产精品99久久久久久久久| 国产精品亚发布| 亚洲精品免费一区二区三区| 激情综合网激情| 午夜国产精品视频| 亚洲视频网站在线观看| 久久亚洲二区| 久久久久久色| 国产精品一香蕉国产线看观看| 亚洲高清视频一区| 黄网站免费久久| 欧美亚洲免费| 欧美一区精品| 国产精品另类一区| 亚洲精品久久视频| 亚洲精品免费在线| 免费观看亚洲视频大全| 久久精品在线观看| 国产精品视频精品视频| 日韩一级精品视频在线观看| 亚洲日本va在线观看| 久久综合久色欧美综合狠狠 | 国产欧美日韩一区| 正在播放亚洲一区| 一区二区三区不卡视频在线观看 | 欧美一级专区免费大片| 国产精品家教| 亚洲无线视频| 亚洲欧美日韩中文播放| 国产精品户外野外| 亚洲视频中文| 欧美一区二区在线播放| 国产乱码精品一区二区三区忘忧草| 一本色道久久加勒比88综合| 中文精品99久久国产香蕉| 欧美日韩性生活视频| 在线一区二区视频| 亚洲欧美日韩久久精品| 国产精品一区二区久久精品| 亚洲欧美一区二区原创| 亚洲一级片在线观看| 欧美日韩一卡| 亚洲制服av| 久久久xxx| 黄色精品免费| 欧美成人免费大片| 亚洲免费观看高清完整版在线观看熊 | 欧美日韩免费在线| 中文成人激情娱乐网| 性高湖久久久久久久久| 国内激情久久| 欧美激情一区二区三区在线视频观看| 亚洲人成艺术| 欧美一区二区三区四区夜夜大片| 国产亚洲欧美一级| 麻豆精品精品国产自在97香蕉| 91久久久亚洲精品| 性欧美8khd高清极品| 亚洲国产成人午夜在线一区| 欧美日精品一区视频| 久久精品国产999大香线蕉| 亚洲国产片色| 欧美在线视频全部完| 亚洲国产另类精品专区| 国产精品久久久久7777婷婷| 欧美影院视频| 亚洲美女91| 久久最新视频| 亚洲欧美日韩国产成人精品影院 | 性久久久久久久| 亚洲国内自拍| 国产九区一区在线| 欧美黄色aaaa| 欧美综合二区| 亚洲私人影吧| 亚洲国产99| 久久久噜噜噜久久久| 亚洲一区激情| 亚洲人体大胆视频| 国内精品模特av私拍在线观看| 欧美剧在线观看| 久久九九免费视频| 亚洲欧美国内爽妇网| 亚洲伦理在线观看| 欧美成人一区二区三区片免费| 亚洲欧美精品一区| 一区二区三区精品久久久| 亚洲国产精品成人精品| 国产视频观看一区| 国产精品午夜av在线| 欧美日韩在线亚洲一区蜜芽| 欧美韩国日本一区| 免费成年人欧美视频| 久久成人一区二区|