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

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>
            欧美日韩亚洲国产一区| 久久久99久久精品女同性| 噜噜爱69成人精品| 久久久亚洲综合| 一区二区三区我不卡| 久久一综合视频| 久久久青草婷婷精品综合日韩| 国产日韩亚洲| 麻豆精品在线观看| 欧美国产精品人人做人人爱| 欧美1区2区3区| 在线亚洲欧美| 销魂美女一区二区三区视频在线| 欧美性事在线| 久久亚洲图片| 欧美三级电影网| 欧美一区二区久久久| 久久人人97超碰人人澡爱香蕉| 亚洲欧洲中文日韩久久av乱码| 亚洲欧洲另类| 国产欧美日韩不卡免费| 欧美多人爱爱视频网站| 欧美日韩精品一区二区三区四区| 久久成人资源| 欧美凹凸一区二区三区视频| 亚洲欧美日韩国产| 久久亚洲春色中文字幕久久久| 亚洲视频在线一区观看| 久久精品国产亚洲5555| 一本到高清视频免费精品| 午夜精彩视频在线观看不卡| 亚洲级视频在线观看免费1级| 亚洲一区国产| 亚洲日本激情| 欧美一区二区三区免费视频| 宅男噜噜噜66一区二区| 久久久欧美精品| 午夜在线观看免费一区| 欧美国产一区在线| 久久香蕉国产线看观看网| 欧美日韩免费高清一区色橹橹| 裸体歌舞表演一区二区| 国产精品红桃| 亚洲精品四区| 亚洲人成网站精品片在线观看| 亚洲一区欧美激情| 亚洲麻豆视频| 久久久99国产精品免费| 欧美中文在线观看国产| 欧美日韩在线一二三| 米奇777在线欧美播放| 国产精品自拍在线| av成人福利| 亚洲视频www| 欧美成人dvd在线视频| 久久综合中文色婷婷| 国产精品国产自产拍高清av王其| 亚洲精品系列| 亚洲国产福利在线| 久久免费视频一区| 久久综合久久综合久久| 国产精品久久久久久模特 | 欧美日韩1080p| 亚洲第一免费播放区| 在线视频国内自拍亚洲视频| 久久蜜桃资源一区二区老牛| 久久久亚洲一区| 一区二区三区中文在线观看 | 欧美电影免费| 亚洲国产毛片完整版| 久久综合999| 亚洲国产高潮在线观看| 亚洲美女av网站| 欧美精品乱人伦久久久久久 | 亚洲精品免费观看| 在线视频精品一区| 国产精品二区二区三区| 亚洲一区二区在线免费观看| 久久久久综合| 亚洲国产精品成人va在线观看| 久久躁狠狠躁夜夜爽| 欧美激情一区二区三区高清视频 | 亚洲你懂的在线视频| 国产精品欧美经典| 欧美在线网站| 亚洲国产精品女人久久久| 一本色道久久综合亚洲精品不| 欧美三级在线播放| 欧美一级网站| 亚洲二区在线| 亚洲你懂的在线视频| 伊人成人开心激情综合网| 欧美激情免费在线| 亚洲小说区图片区| 麻豆91精品| 亚洲视频在线看| 激情校园亚洲| 欧美日韩一区二区三区四区五区| 亚洲一区www| 欧美电影打屁股sp| 亚洲欧美日韩综合| 国外成人网址| 欧美日韩一区二区三区在线看 | 女生裸体视频一区二区三区| 夜夜精品视频一区二区| 国产欧美日韩免费| 欧美另类人妖| 久久久精品tv| 夜夜嗨一区二区三区| 免费欧美在线视频| 午夜激情综合网| 亚洲激情女人| 国产乱码精品| 欧美日韩国产另类不卡| 久久精品视频免费播放| 亚洲视频在线一区观看| 欧美激情欧美激情在线五月| 欧美永久精品| 一区二区三区精品久久久| 国产一级久久| 欧美日韩国产在线观看| 亚洲经典三级| 国模私拍一区二区三区| 国产精品白丝jk黑袜喷水| 嫩草伊人久久精品少妇av杨幂| 午夜一区不卡| 亚洲深夜av| 一区二区欧美国产| 亚洲美女在线一区| 亚洲毛片在线观看.| 欧美成人激情视频| 久久人人超碰| 久久久中精品2020中文| 小处雏高清一区二区三区| 一区二区电影免费在线观看| 亚洲欧洲精品一区二区| 亚洲高清不卡在线观看| 国产亚洲一级高清| 国产欧美va欧美va香蕉在| 国产精品夜色7777狼人 | 欧美成人午夜激情视频| 美女视频黄a大片欧美| 久久午夜国产精品| 久久久最新网址| 久久综合五月天婷婷伊人| 久久精品在这里| 欧美在线一区二区| 久久久久久久久岛国免费| 久久国产日本精品| 久久免费视频这里只有精品| 久久久九九九九| 久久久噜噜噜久久人人看| 久久精品国产久精国产爱| 久久久久国产一区二区| 开心色5月久久精品| 蘑菇福利视频一区播放| 欧美国产日韩xxxxx| 欧美精品一区二区三区一线天视频 | 亚洲欧洲日夜超级视频| 亚洲欧洲日产国码二区| 亚洲午夜电影网| 欧美一区二区日韩一区二区| 久久久久久有精品国产| 久久夜色精品国产| 亚洲韩国日本中文字幕| 亚洲视频精品| 亚洲欧美另类在线观看| 久久成人综合视频| 欧美刺激午夜性久久久久久久| 欧美日本免费| 国产一区二区三区在线观看精品 | 亚洲欧美成人一区二区在线电影| 亚洲欧美大片| 麻豆精品一区二区综合av | 亚洲性线免费观看视频成熟| 欧美一二三视频| 欧美激情视频一区二区三区免费| 欧美欧美全黄| 国产专区综合网| 一本在线高清不卡dvd| 久久动漫亚洲| 亚洲精品乱码久久久久久| 亚洲欧美日韩在线观看a三区| 久久一区国产| 国产精品一区在线观看| 亚洲国产精品va在线看黑人动漫| 亚洲免费伊人电影在线观看av| 久久综合综合久久综合| 一区二区三区四区在线| 另类春色校园亚洲| 国产精品日韩在线一区| 亚洲精品久久久一区二区三区| 这里只有精品丝袜| 久久婷婷一区| 亚洲欧美日韩综合aⅴ视频| 欧美成人精品1314www| 国产综合久久| 午夜精品久久久久久久99黑人| 亚洲第一中文字幕| 久久精品女人|