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

            將排序二叉樹轉換成雙向鏈表

            Posted on 2014-01-03 00:41 whspecial 閱讀(3613) 評論(0)  編輯 收藏 引用 所屬分類: 算法&&數據結構
            將排序二叉樹轉化成雙向鏈表,應該是一道很常見的面試題目,網上的實現比較多,有用遞歸也有用中序遍歷法的。看到一位外國友人的實現,還是比較清晰的,思路如下:
            1,如果左子樹不為null,處理左子樹
               1.a)遞歸轉化左子樹為雙向鏈表;
               1.b)找出根結點的前驅節點(是左子樹的最右的節點)
               1.c)將上一步找出的節點和根結點連接起來
            2,如果右子樹不為null,處理右子樹(和上面的很類似)
               1.a)遞歸轉化右子樹為雙向鏈表;
               1.b)找出根結點的后繼節點(是右子樹的最左的節點)
               1.c)將上一步找出的節點和根結點連接起來
            3,找到最左邊的節點并返回

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

            下面是代碼實現:
            bintree2listUtil函數返回的node* 是root節點,bintree2list函數返回的是頭節點
            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久久精品免费看蜜桃| 国产真实乱对白精彩久久| 久久无码专区国产精品发布| 欧美日韩久久中文字幕| 国产精品久久精品| 久久人人爽人人爽人人片AV东京热| 亚洲午夜无码AV毛片久久| 久久久亚洲欧洲日产国码aⅴ| 国产精品丝袜久久久久久不卡| 亚洲国产成人久久综合一区77| 俺来也俺去啦久久综合网| 久久亚洲国产成人影院网站| 国产精品美女久久久久| 久久精品桃花综合| 欧美激情精品久久久久| 久久久久se色偷偷亚洲精品av| 欧美精品一本久久男人的天堂| 亚洲AV无码久久精品成人| 久久免费视频一区| 国产精品成人精品久久久| 久久婷婷五月综合色奶水99啪 | 国产精品久久毛片完整版| 色综合久久天天综线观看| 国内精品久久国产大陆| 久久久久精品国产亚洲AV无码| 久久久受www免费人成| 久久综合九色综合久99| 99久久99这里只有免费费精品 | 久久久久久国产精品免费无码| 天堂无码久久综合东京热| 国产农村妇女毛片精品久久| 久久久无码精品亚洲日韩蜜臀浪潮 | 久久人人爽人人爽人人片av麻烦| 亚洲乱亚洲乱淫久久| 精品人妻久久久久久888| 久久久久亚洲AV成人网人人网站| 久久久久国产精品麻豆AR影院| 久久婷婷久久一区二区三区| 99久久国产综合精品麻豆| 97久久精品无码一区二区天美 | 欧美久久天天综合香蕉伊|