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

    A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties:

    • The left subtree of a node contains only nodes with keys less than the node's key.
    • The right subtree of a node contains only nodes with keys greater than or equal to the node's key.
    • Both the left and right subtrees must also be binary search trees.

      A Complete Binary Tree (CBT) is a tree that is completely filled, with the possible exception of the bottom level, which is filled from left to right.

      Now given a sequence of distinct non-negative integer keys, a unique BST can be constructed if it is required that the tree must also be a CBT. You are supposed to output the level order traversal sequence of this BST.

      Input Specification:

      Each input file contains one test case. For each case, the first line contains a positive integer N (1000). Then N distinct non-negative integer keys are given in the next line. All the numbers in a line are separated by a space and are no greater than 2000.

      Output Specification:

      For each test case, print in one line the level order traversal sequence of the corresponding complete binary search tree. All the numbers in a line must be separated by a space, and there must be no extra space at the end of the line.

      Sample Input:

      10
      1 2 3 4 5 6 7 8 9 0
      

      Sample Output:

      6 3 8 1 5 7 9 0 2 4
      
    我的答案:
     1 #include <stdio.h>
     2 #include <stdlib.h>
     3 #include <unistd.h>
     4 
     5 int b[2048];
     6 int pos = 0;
     7 
     8 void sort(int *arr, int num)
     9 {
    10     int i, j;
    11     int temp;
    12 
    13     for(i=0;i<num;i++) {
    14         for(j=i;j<num;j++) {
    15             if(arr[i]>arr[j]) {
    16                 temp = arr[j];
    17                 arr[j] = arr[i];
    18                 arr[i] = temp;
    19             }
    20         }
    21     }
    22 }
    23 
    24 void mid_tree(int root, int N, int a[])
    25 {
    26     if(root<=N) {
    27         mid_tree(2*root, N, a);
    28         b[root] =  a[pos++];
    29         mid_tree(2*root+1, N, a);
    30     }
    31 }
    32 
    33 int main()
    34 {
    35     int N, i;
    36     int data[2048];
    37     scanf("%d
    ", &N);
    38     for(i=0;i<N;i++) {
    39         scanf("%d", &data[i]);        
    40     }
    41     sort(data, N);
    42     mid_tree(1, N, data);
    43     for(i=1;i<=N;i++) {
    44         if(i==1) {
    45             printf("%d", b[i]);
    46         } else {
    47             printf(" %d", b[i]);
    48         }
    49     }
    50 
    51     return 0;
    52 }
    无欲速,无见小利。欲速,则不达;见小利,则大事不成。
  • 相关阅读:
    cf1058c 暴力
    cf1058b 点是否在一个矩形里
    cf1058E 思维 前缀处理 位运算
    Codeforces Round #622 (Div. 2)C(单调栈,DP)
    Codeforces Round #623 (Div. 1, based on VK Cup 2019-2020
    Atcoder Beginner Contest 156E(隔板法,组合数学)
    【PAT甲级】1117 Eddington Number (25分)
    【PAT甲级】1116 Come on! Let's C (20分)
    【PAT甲级】1115 Counting Nodes in a BST (30分)(二叉查找树)
    Codeforces Round #621 (Div. 1 + Div. 2)E(二分查找,枚举分界点,容斥原理)
  • 原文地址:https://www.cnblogs.com/ch122633/p/8805778.html
Copyright © 2011-2022 走看看