• <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 閱讀(186) 評論(0)  編輯 收藏 引用 所屬分類: M_面試題集錦

            導航

            <2010年9月>
            2930311234
            567891011
            12131415161718
            19202122232425
            262728293012
            3456789

            統計

            常用鏈接

            留言簿(1)

            隨筆分類

            隨筆檔案

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            99久久精品国产综合一区 | 久久精品国产亚洲AV不卡| 久久婷婷五月综合97色直播| 久久久精品视频免费观看| 久久影院久久香蕉国产线看观看| 午夜肉伦伦影院久久精品免费看国产一区二区三区 | yy6080久久| 精品熟女少妇av免费久久| 精品久久久久久国产三级 | 日本强好片久久久久久AAA| 91久久精一区二区三区大全| 国内精品欧美久久精品| 亚洲中文字幕无码久久精品1| MM131亚洲国产美女久久| 久久午夜无码鲁丝片午夜精品| 精品久久亚洲中文无码| 中文字幕久久欲求不满| 亚洲国产精品无码久久98| 很黄很污的网站久久mimi色| 婷婷久久香蕉五月综合加勒比| 久久精品免费大片国产大片| 久久久久亚洲Av无码专| 亚洲AⅤ优女AV综合久久久| 久久91精品国产91久久小草 | 久久噜噜电影你懂的| 久久精品午夜一区二区福利| 久久久久久亚洲精品无码| 久久国产精品99国产精| 2021久久精品免费观看| 久久久久九国产精品| 国产精品午夜久久| 97超级碰碰碰久久久久| 亚洲精品乱码久久久久久蜜桃不卡| 日本精品久久久中文字幕| 久久国产精品无码HDAV| 精品国产乱码久久久久久1区2区 | 国产三级观看久久| 久久免费国产精品一区二区| 免费国产99久久久香蕉| 亚洲国产精品一区二区久久| 国产精品美女久久久久久2018|