• <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 閱讀(3622) 評論(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| 午夜精品久久久久久久| 亚洲精品乱码久久久久66| 久久91精品国产91久久小草| 欧美亚洲国产精品久久蜜芽| 欧美久久久久久| 99久久婷婷国产综合亚洲| 理论片午午伦夜理片久久| 久久久久久久久无码精品亚洲日韩| 国产成人久久精品麻豆一区 | 亚洲AV无一区二区三区久久| 99久久无码一区人妻a黑| 97视频久久久| 久久精品亚洲乱码伦伦中文| 久久综合给合久久国产免费| 久久久久99精品成人片牛牛影视 | 国产成人精品白浆久久69| 久久国产精品一区| 国产精品久久久亚洲| 久久久久av无码免费网| 国产高潮久久免费观看| 久久国产精品99久久久久久老狼 | 思思久久99热免费精品6| 国产精品视频久久| 99国产欧美久久久精品蜜芽| 国产精品成人久久久| 久久综合久久鬼色| 久久性精品| 久久综合鬼色88久久精品综合自在自线噜噜 | 久久午夜免费视频| 久久久久久国产精品免费免费| 精品国产综合区久久久久久| 亚洲成人精品久久| 色综合久久中文综合网| 激情久久久久久久久久| 久久精品国产一区二区电影| 久久国产视频99电影| 日韩十八禁一区二区久久| 亚洲精品久久久www| 精品久久久无码21p发布|