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

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

            下面是代碼實(shí)現(xiàn):
            bintree2listUtil函數(shù)返回的node* 是root節(jié)點(diǎn),bintree2list函數(shù)返回的是頭節(jié)點(diǎn)
            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);

            只有注冊用戶登錄后才能發(fā)表評論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


            精品久久久久中文字幕日本| 欧美久久天天综合香蕉伊| 99久久精品免费看国产一区二区三区 | 久久免费大片| 亚洲国产高清精品线久久| 精品熟女少妇AV免费久久| 国内精品久久久久久99| 久久国产精品偷99| 国产精品美女久久福利网站| 日韩人妻无码精品久久久不卡| 久久国产免费观看精品3| 国产—久久香蕉国产线看观看| 久久精品免费全国观看国产| 99久久精品午夜一区二区 | 久久久久久伊人高潮影院| 久久棈精品久久久久久噜噜| 国产精品嫩草影院久久| 狠狠色婷婷久久一区二区| 久久综合丝袜日本网| 国内精品久久久久久久久电影网| 九九99精品久久久久久| 国产精品乱码久久久久久软件 | 久久人妻无码中文字幕| 久久香蕉一级毛片| 思思久久好好热精品国产| 国内精品久久久久影院免费| 亚洲美日韩Av中文字幕无码久久久妻妇 | 国产成人无码精品久久久久免费| 久久久久免费精品国产| 欧美国产精品久久高清| 久久久中文字幕| 午夜不卡久久精品无码免费| 久久久久这里只有精品| 97精品伊人久久大香线蕉app| 中文字幕无码久久久| 99久久国产热无码精品免费久久久久| 精品久久久无码人妻中文字幕| 久久97久久97精品免视看| 91精品国产色综合久久| 久久99热这里只有精品66| 久久男人中文字幕资源站|