04-树6 Complete Binary Search Tree (30 分)

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

我的答案:

#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;

#define MaxSize 1005
int sortNum[MaxSize] = {0};
int CBTreeNode[MaxSize] = {0};
int countNum = 0;
void CreatCBTree(int root,int N)
{
    if(root > N)
        return;
    int left = root * 2;
    int right = root * 2 + 1;
    CreatCBTree(left,N);        //中序遍历LGR从小到大 小的先
    CBTreeNode[root] = sortNum[countNum++];
    CreatCBTree(right,N);
}

int main()
{
    int N;
    scanf("%d",&N);
    for(int i = 0; i < N; i++) 
        scanf("%d",&sortNum[i]);
    
    sort(sortNum,sortNum + N);//按从小到大排序 
    CreatCBTree(1,N);
    for(int i = 1; i <= N; i++) {
        if(i != N)
            printf("%d ",CBTreeNode[i]);
        else
            printf("%d",CBTreeNode[i]);
    }
    return 0;
}

感悟

好难,我现在也没想明白
中序的中序就是层序的下标?为啥?

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi阅读 7,452评论 0 10
  • 我在马戏团认识了个新朋友,他一直沉迷宇宙,他非常喜欢像UFO那样的东西。 有一天,我的朋友不小心被UFO吸走,后来...
    棒丁阅读 283评论 0 1
  • 来东莞这么多天了,只觉得很累。 当我在东莞东火车站拨打朋友电话却没人理时,我独自抗那烈日火娇娘。 当我在东莞东汽车...
    如意路阅读 1,433评论 0 0
  • 【执子之手】儿童学习力六期 践行记录20170527Day12 1.今天复习了昨天的两首手指谣,读了两遍后宝宝拿着...
    cancan妈阅读 100评论 0 0
  • 很多电视剧里面都有这么个经典场景: 女生受了老板的委屈之后,去和男朋友抱怨,情绪激动不已,噼里啪啦说了一通。男生一...
    安鑫鑫森阅读 1,902评论 2 4