• <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>
            posts - 15, comments - 10, trackbacks - 0, articles - 0
            將排序二叉樹轉(zhuǎn)化成雙向鏈表,應該是一道很常見的面試題目,網(wǎng)上的實現(xiàn)比較多,有用遞歸也有用中序遍歷法的。看到一位外國友人的實現(xiàn),還是比較清晰的,思路如下:
            1,如果左子樹不為null,處理左子樹
               1.a)遞歸轉(zhuǎn)化左子樹為雙向鏈表;
               1.b)找出根結點的前驅(qū)節(jié)點(是左子樹的最右的節(jié)點)
               1.c)將上一步找出的節(jié)點和根結點連接起來
            2,如果右子樹不為null,處理右子樹(和上面的很類似)
               1.a)遞歸轉(zhuǎn)化右子樹為雙向鏈表;
               1.b)找出根結點的后繼節(jié)點(是右子樹的最左的節(jié)點)
               1.c)將上一步找出的節(jié)點和根結點連接起來
            3,找到最左邊的節(jié)點并返回

            附上國外友人的鏈接:http://www.geeksforgeeks.org/in-place-convert-a-given-binary-tree-to-doubly-linked-list/

            下面是代碼實現(xiàn):
            bintree2listUtil函數(shù)返回的node* 是root節(jié)點,bintree2list函數(shù)返回的是頭節(jié)點
            This is the core function to convert Tree to list. This function follows
              steps 1 and 2 of the above algorithm */
            node* bintree2listUtil(node* root)
            {
                // Base case
                if (root == NULL)
                    return root;
             
                // Convert the left subtree and link to root
                if (root->left != NULL)
                {
                    // Convert the left subtree
                    node* left = bintree2listUtil(root->left);
             
                    // Find inorder predecessor. After this loop, left
                    // will point to the inorder predecessor
                    for (; left->right!=NULL; left=left->right);
             
                    // Make root as next of the predecessor
                    left->right = root;
             
                    // Make predecssor as previous of root
                    root->left = left;
                }
             
                // Convert the right subtree and link to root
                if (root->right!=NULL)
                {
                    // Convert the right subtree
                    node* right = bintree2listUtil(root->right);
             
                    // Find inorder successor. After this loop, right
                    // will point to the inorder successor
                    for (; right->left!=NULL; right = right->left);
             
                    // Make root as previous of successor
                    right->left = root;
             
                    // Make successor as next of root
                    root->right = right;
                }
             
                return root;
            }
             
            // The main function that first calls bintree2listUtil(), then follows step 3
            //  of the above algorithm
            node* bintree2list(node *root)
            {
                // Base case
                if (root == NULL)
                    return root;
             
                // Convert to DLL using bintree2listUtil()
                root = bintree2listUtil(root);
             
                // bintree2listUtil() returns root node of the converted
                // DLL.  We need pointer to the leftmost node which is
                // head of the constructed DLL, so move to the leftmost node
                while (root->left != NULL)
                    root = root->left;
             
                return (root);
            99久久精品国产一区二区蜜芽| 99久久婷婷国产一区二区| 午夜视频久久久久一区| 国内精品免费久久影院| 国产精品综合久久第一页| 色偷偷88欧美精品久久久 | 国产精品美女久久久久久2018| 狠狠色丁香久久综合五月| 久久婷婷人人澡人人| 久久夜色精品国产噜噜噜亚洲AV| 九九99精品久久久久久| 一本色综合久久| 国产农村妇女毛片精品久久| 亚洲午夜久久久久久久久电影网| 国产精品无码久久四虎| 午夜精品久久久久久影视riav| 狠狠色丁香婷婷综合久久来| 久久热这里只有精品在线观看| 青青青国产精品国产精品久久久久 | 狠狠精品久久久无码中文字幕| 久久久精品久久久久久| 国产aⅴ激情无码久久| 九九久久精品国产| 久久国产色AV免费观看| 久久久精品国产免大香伊| 亚洲美日韩Av中文字幕无码久久久妻妇 | 狠狠综合久久AV一区二区三区| 亚洲精品国精品久久99热一| 亚洲国产精品成人AV无码久久综合影院 | 久久99精品久久久久久水蜜桃| 亚洲欧美日韩久久精品| 国产免费久久精品99久久| 久久国产精品国产自线拍免费| 少妇久久久久久久久久| 久久久精品人妻一区二区三区蜜桃| 欧美国产精品久久高清| 亚洲国产精品狼友中文久久久| 久久er国产精品免费观看8| 精品久久久久中文字幕一区| 国内精品久久久久久久亚洲| 久久精品成人影院|