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

            附上國(guó)外友人的鏈接: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);

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


            久久亚洲av无码精品浪潮| 久久夜色精品国产噜噜麻豆| 精品免费tv久久久久久久| 国产99久久久久久免费看| 久久久久久无码国产精品中文字幕| 精品乱码久久久久久夜夜嗨| 久久亚洲国产精品成人AV秋霞| 精品久久久久久亚洲精品| 久久国产精品无码网站| 亚洲AV无码久久精品色欲 | 久久久久久一区国产精品| 综合久久精品色| 国产精品九九九久久九九| 久久久久久国产精品无码下载 | 欧洲精品久久久av无码电影| 久久精品国产秦先生| 久久久久久久精品妇女99| 久久国产乱子伦精品免费强| 亚洲精品无码专区久久同性男| 久久丫精品国产亚洲av不卡| 久久久久亚洲AV无码专区桃色 | 亚洲午夜精品久久久久久app| 99久久国语露脸精品国产| 性做久久久久久久久浪潮| 嫩草影院久久99| 日韩人妻无码精品久久久不卡| 亚洲精品97久久中文字幕无码| 亚洲一本综合久久| 久久国产精品99精品国产987| 久久久久久精品成人免费图片| 久久亚洲2019中文字幕| 18岁日韩内射颜射午夜久久成人| 亚洲精品无码久久千人斩| 狠狠色丁香婷婷综合久久来来去| AV色综合久久天堂AV色综合在| 色综合久久久久无码专区| 久久乐国产综合亚洲精品| 无码任你躁久久久久久| 伊色综合久久之综合久久| 亚洲欧洲久久久精品| 久久人人爽人人爽人人片AV高清 |