• <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>

            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菜鳥 閱讀(330) 評論(0)  編輯 收藏 引用 所屬分類: 算法/數據結構
            久久久WWW成人免费毛片| 天天爽天天爽天天片a久久网| 久久久亚洲精品蜜桃臀| 国产精品美女久久福利网站| 久久婷婷五月综合97色一本一本 | 久久久黄片| 亚洲欧美日韩中文久久| 国产精品久久久久久| 久久综合伊人77777| 99精品久久久久久久婷婷| 国产高潮久久免费观看| 天天爽天天狠久久久综合麻豆| 国产伊人久久| 久久99国内精品自在现线| 久久综合九色综合久99| 国产成人无码精品久久久久免费 | 国产成人精品久久一区二区三区| 久久99精品久久久久久不卡| 久久天天躁狠狠躁夜夜avapp| 亚州日韩精品专区久久久| 久久亚洲国产中v天仙www| 伊人久久综合精品无码AV专区| 色婷婷噜噜久久国产精品12p| 久久99国产精一区二区三区| 色婷婷综合久久久久中文| 三级片免费观看久久| 91精品日韩人妻无码久久不卡| 蜜臀av性久久久久蜜臀aⅴ麻豆 | 久久精品国产第一区二区| 久久精品一区二区国产| 久久久久亚洲av无码专区喷水 | 国产精品99久久精品| 一本一本久久aa综合精品| 久久久久人妻一区二区三区| 性做久久久久久免费观看| 亚洲伊人久久成综合人影院| 四虎影视久久久免费观看| 色综合久久中文字幕综合网| 久久精品极品盛宴观看| 一97日本道伊人久久综合影院| 久久青青草视频|