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

            導航

            <2011年8月>
            31123456
            78910111213
            14151617181920
            21222324252627
            28293031123
            45678910

            統計

            常用鏈接

            留言簿(1)

            隨筆分類

            隨筆檔案

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            国产99久久久久久免费看| 久久婷婷国产综合精品| 91麻精品国产91久久久久 | 久久久精品午夜免费不卡| 久久精品国产亚洲av水果派 | 91精品国产91热久久久久福利| 国产精品国色综合久久| 国内精品久久久久影院网站| 久久亚洲精品国产精品婷婷| 久久青青色综合| 东京热TOKYO综合久久精品| 激情综合色综合久久综合| 无码AV中文字幕久久专区| 99久久国产亚洲高清观看2024| 思思久久99热免费精品6| 久久人人爽人人爽人人AV东京热| 久久精品国产只有精品66 | 久久久久一本毛久久久| 久久综合亚洲鲁鲁五月天| 久久99热国产这有精品| 精品无码久久久久国产动漫3d| 青青草原1769久久免费播放| 精品熟女少妇AV免费久久 | 久久精品无码专区免费| 亚洲va中文字幕无码久久| 久久综合精品国产一区二区三区| 久久亚洲国产成人精品性色| 欧美日韩中文字幕久久久不卡 | 亚洲午夜久久影院| 色欲综合久久中文字幕网| 亚洲国产精品综合久久网络| 久久久久久无码国产精品中文字幕| 好久久免费视频高清| 久久久久久国产精品无码超碰| 久久久午夜精品福利内容| 久久99精品国产麻豆蜜芽| 久久午夜电影网| 狠狠精品久久久无码中文字幕 | 日批日出水久久亚洲精品tv| 国产99久久久久久免费看| 久久99精品久久久久久噜噜|