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

            A Za, A Za, Fighting...

            堅信:勤能補拙

            求二元查找樹的鏡像

            題目來源: http://blog.163.com/prevBlogPerma.do?host=zhedahht&srl=2541117420072159363370&mode=prev

            題目:輸入一顆二元查找樹,將該樹轉換為它的鏡像,即在轉換后的二元查找樹中,左子樹的結點都大于右子樹的結點。用遞歸和循環兩種方法完成樹的鏡像轉換。

            例如輸入:

                 8
                /  \
              6      10
             /\       /\
            5  7    9   11

            輸出:

                  8
                /  \
              10    6
             /\      /\
            11  9  7  5

            #include<stdio.h>
            #include
            <stdlib.h>
            #include
            <string.h>

            struct Node {
                
            int value;
                
            struct Node *left;
                
            struct Node *right;
            };

            void
            bst_preorder(
            struct Node *root)
            {
                
            if(root == NULL)
                    
            return;

                printf(
            "%d\t", root->value);
                bst_preorder(root
            ->left);
                bst_preorder(root
            ->right);
            }

            void
            bst_mirror_recursive(
            struct Node *root) /* easy */
            {
                
            if(root == NULL)
                    
            return;

                
            struct Node *ptr = root->left;
                root
            ->left = root->right;
                root
            ->right = ptr;

                bst_mirror_recursive(root
            ->left);
                bst_mirror_recursive(root
            ->right);
            }

            /* STACK : naive */
            #define STACK_SIZE 101
            struct Stack {
                
            void *data[STACK_SIZE];
                
            int top;
            };

            void
            stack_pop(
            struct Stack *stack)
            {
                
            if((stack->top) >= 0)
                    
            --(stack->top);
            }

            void *
            stack_top(
            struct Stack *stack)
            {
                
            if((stack->top) >= 0)
                    
            return stack->data[stack->top];

                
            return NULL;
            }

            void
            stack_push(
            struct Stack *stack, void *entity)
            {
                stack
            ->data[++(stack->top)] = entity;
            }

            int
            stack_isempty(
            struct Stack *stack)
            {
                
            return (stack->top) < 0;
            }

            void
            bst_mirror_nonrecursive(
            struct Node *root, struct Stack *aux_stack) /* stack used : good method */
            {
                stack_push(aux_stack, root);
                
            while(!stack_isempty(aux_stack)) {
                    
            struct Node *node = (struct Node *)stack_top(aux_stack);

                    
            struct Node *ptr = node->left;
                    node
            ->left = node->right;
                    node
            ->right = ptr;

                    stack_pop(aux_stack);
                    
            if(node->left)
                        stack_push(aux_stack, node
            ->left);
                    
            if(node->right)
                        stack_push(aux_stack, node
            ->right);
                }
            }

            int
            main(
            int argc, char **argv)
            {
                
            struct Node a = {5, NULL, NULL};
                
            struct Node b = {7, NULL, NULL};
                
            struct Node c = {9, NULL, NULL};
                
            struct Node d = {11, NULL, NULL};
                
            struct Node e = {6&a, &b};
                
            struct Node f = {10&c, &d};
                
            struct Node g = {8&e, &f};

                bst_preorder(
            &g);
                printf(
            "\n");
                bst_mirror_recursive(
            &g);
                bst_preorder(
            &g);
                printf(
            "\n");

                bst_mirror_recursive(
            &g);
                bst_preorder(
            &g);
                printf(
            "\n");
                
            struct Stack aux = {{0}, -1};
                bst_mirror_nonrecursive(
            &g, &aux);
                bst_preorder(
            &g);
                printf(
            "\n");

                
            return 0;
            }

             




            posted on 2011-06-01 19:58 simplyzhao 閱讀(187) 評論(0)  編輯 收藏 引用 所屬分類: M_面試題集錦

            導航

            <2011年6月>
            2930311234
            567891011
            12131415161718
            19202122232425
            262728293012
            3456789

            統計

            常用鏈接

            留言簿(1)

            隨筆分類

            隨筆檔案

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            91精品国产高清91久久久久久| 久久精品卫校国产小美女| 久久精品桃花综合| 一本色综合网久久| 国产精品成人精品久久久 | 久久亚洲国产精品123区| 欧洲国产伦久久久久久久| 2021国产精品久久精品| 亚洲国产精品久久久久久| 伊人色综合九久久天天蜜桃| 久久精品国产一区二区三区日韩| 亚洲国产日韩欧美综合久久| 亚洲AV日韩精品久久久久| 久久久精品国产Sm最大网站| 色偷偷888欧美精品久久久| 久久精品国产亚洲精品2020| 开心久久婷婷综合中文字幕| 久久久久久国产精品无码超碰| 国内精品久久久久影院网站| 9191精品国产免费久久| 97精品伊人久久大香线蕉| 国产欧美久久一区二区| 久久精品中文字幕一区| 久久综合九色综合欧美就去吻| 国产精品久久久久影院色| 综合网日日天干夜夜久久| 中文字幕亚洲综合久久菠萝蜜| 久久99免费视频| 精品久久久久久亚洲| 97久久精品无码一区二区天美| 久久无码AV中文出轨人妻| 久久久久国产一级毛片高清板| 久久国产精品成人免费| MM131亚洲国产美女久久| 久久综合综合久久97色| 99久久99这里只有免费的精品| 久久亚洲精品无码VA大香大香| 香蕉久久影院| 久久国语露脸国产精品电影| 久久久久久精品久久久久| 久久精品aⅴ无码中文字字幕不卡|