zoukankan      html  css  js  c++  java
  • 04-树6 Complete Binary Search Tree

      此题要求根据输入数据得到该数据 的 完全二叉搜索树的层序遍历结果。那是不是要建完全二叉搜索树?怎么建?或者不建树得到结果?功力不够,都不会啊。

      度娘一下,被别人的实现吓到了,感觉太复杂太长了,头疼~

      然而又被别人的实现惊艳到了,太厉害了~

      参考:  https://blog.csdn.net/Roland_WuZF/article/details/49389995

     1 #include <stdio.h>
     2 #include <stdlib.h>
     3 
     4 #define MaxSize 1001
     5 
     6 int Seq[MaxSize], LevelSeq[MaxSize];
     7 int j = 0;
     8 
     9 int compare(const void *a, const void *b);
    10 void  InOrderStore(int root, int N);
    11 
    12 int main()
    13 {
    14     int i, N;
    15     scanf("%d", &N);
    16     
    17     for ( i = 0; i < N; i++ ) {
    18         scanf("%d", &Seq[i]);
    19     }
    20     // 排序 (increase) 得到 中序遍历的顺序 
    21     qsort(Seq, N, sizeof(int), compare);
    22     
    23     InOrderStore(1, N);
    24     
    25     printf("%d", LevelSeq[1]);
    26     for ( i = 2; i <= N; i++ ) {
    27         printf(" %d", LevelSeq[i]);
    28     }
    29     
    30     return 0;
    31 }
    32 /*
    33 以 中序遍历 的方式 使用数组LevelSeq存储 中序遍历 得到的数组Seq, 
    34 */
    35 void  InOrderStore(int root, int N)
    36 {
    37     if ( root <= N ) {
    38         InOrderStore(root*2, N);
    39         LevelSeq[root] = Seq[j++];
    40         InOrderStore(root*2+1, N);
    41     } 
    42 }
    43 
    44 int compare(const void *a, const void *b)
    45 {
    46     return ( *(int*)a - *(int*)b );
    47 }
  • 相关阅读:
    001-进程与线程
    mysql优化(11)
    mysql优化(十)
    mysql优化(九)
    mysql优化(八)
    mysql优化(七)
    mysql优化(六)
    mysql优化(五)
    mysql优化(四)
    mysql优化(二)
  • 原文地址:https://www.cnblogs.com/wgxi/p/9998551.html
Copyright © 2011-2022 走看看