zoukankan      html  css  js  c++  java
  • 天梯赛练习 L3-010 是否完全二叉搜索树 (30分) 数组建树模拟

    题目分析:

    本题的要求是将n个数依次插入一个空的二叉搜索树(左大右小,且没有重复数字),最后需要输出其层次遍历以及判断是否是完全二叉搜索树,通过观察我们发现, 如果这个树是用数组建立的,那么最后输出的时候只要按编号从大到小就可以输出层序遍历了,此外,对于是否完全二叉树的判断也可以通过直接判断数组对应的值是否为0即可,需要注意的是由于可能出现极端的情况,这里20个数字可能会占用2^20-1的空间,所以数组需要开大一些

    由数组建树代码:

     1 #include<iostream>
     2 #include<stdio.h>
     3 #include<string.h>
     4 using namespace std;
     5 
     6 int k[2500000];
     7 
     8 void createTree(int gen, int x){
     9     if(k[gen] == 0){
    10         k[gen] = x;
    11         return;
    12     }else{
    13         if(x > k[gen]) createTree(gen*2, x);
    14         else createTree(gen*2+1, x);
    15         return;
    16     }
    17 }
    18 
    19 int main(){
    20     int n;
    21     scanf("%d", &n);
    22     memset(k, 0, sizeof(k));
    23     for(int i = 1; i <= n; i++){
    24         int x;
    25         scanf("%d", &x);
    26         createTree(1, x);
    27     }
    28     int flag = 1;
    29     int cnt = 0;
    30     int num = 1;
    31     for(int i = 1; i <= n; i++) num *= 2;
    32     for(int i = 1; i <= num; i++){
    33         if(k[i] != 0){
    34             cnt++;
    35             if(cnt > 1) printf(" ");
    36             printf("%d", k[i]);
    37         }else{
    38             if(cnt < n) flag = 0; 
    39         }
    40     }
    41     printf("
    "); 
    42     if(flag == 1) printf("YES
    ");
    43     else printf("NO
    ");
    44     return 0;
    45 }

     由链式建树代码:

     1 #include<iostream>
     2 #include<stdio.h>
     3 #include<string.h>
     4 #include<queue>
     5 using namespace std;
     6 
     7 int cnt, n;
     8 
     9 struct Node{
    10     int data;
    11     Node* left;
    12     Node* right;
    13 }*root;
    14 
    15 Node* createTree(Node* root, int x){
    16     if(root == NULL){
    17         root = new Node();
    18         root->data = x;
    19         root->left = NULL;
    20         root->right = NULL;
    21         return root;
    22     }
    23     if(x > root->data) root->left = createTree(root->left, x);
    24     else root->right = createTree(root->right, x);
    25     return root;
    26 }
    27 
    28 void run(Node* root){
    29     queue<Node*> q;
    30     cnt = 1;
    31     q.push(root);
    32     int flag = 1;
    33     while(!q.empty()){        //有节点就有数字需要输出 
    34         Node* t = q.front();
    35         q.pop();
    36         if(cnt > 1) printf(" ");
    37         printf("%d", t->data);
    38         if(t->left){
    39             q.push(t->left);
    40             cnt++;
    41         } 
    42         else{
    43             if(cnt < n) flag = 0;
    44         }
    45         if(t->right){
    46             q.push(t->right);    
    47             cnt++;
    48         } 
    49         else{
    50             if(cnt < n) flag = 0;
    51         }
    52     }
    53     printf("
    ");
    54     if(flag == 1) printf("YES
    ");
    55     else printf("NO
    ");
    56 }
    57 
    58 int main(){
    59     scanf("%d", &n);
    60     for(int i = 1; i <= n; i++){
    61         int x;
    62         scanf("%d", &x);
    63         root = createTree(root, x);
    64     }    
    65     run(root);
    66     return 0;
    67 }
  • 相关阅读:
    获取股票行情API 接口
    使用百度地图来展示自定义的GPS点,用pyechart 框架实例
    C 语言基础笔记
    GPS 测试汇总和python GPS 导航地图实现
    用python 来炒股二 BeautifulSoup爬虫信息新闻文章
    Python tkinter 笔记 [pack,place,grid 布局管理]
    RSS 订阅精选 2020
    用python来炒股<三> 炒股交易系统(法则)
    使用python 来实现炒股
    鼠须管输入法的配置介绍
  • 原文地址:https://www.cnblogs.com/YLTFY1998/p/12264470.html
Copyright © 2011-2022 走看看