题目链接
题目分析

输入一组整数(不等),要求构造一棵树,满足同时为BST和完全二叉树
之后输出层序遍历序列

解题思路
  • 思路一:利用BST中序遍历为递增序列
  • 思路二:利用完全二叉树的性质切分序列

思路一 (简单)

1、开一个数组CBT[]用来存储完全二叉树;
2、将输入的数值递增排序;
3、对CBT[]表示的二叉树进行中序遍历,并在遍历过程中将数字从小到大填入数组;
4、由于CBT[]就是按层序来保存完全二叉树的,直接输出即可。

/**********************************
*@ID: 3stone
*@ACM: PAT.A1064 Complete Binary Serach Tree
*@Time: 18/8/13
*@IDE: VSCode 2018 + clang++
***********************************/
#include<cstdio>
#include<algorithm>
using namespace std;const int maxn = 1010;int N, cbt[maxn], seq[maxn];
int key = 1;void in_order(int root) {if(root > N) return;in_order(root * 2); //遍历左子树cbt[root] = seq[key++]; //赋值in_order(root * 2 + 1); //遍历右子树
}int main() {while(scanf("%d", &N) != EOF) {key = 1;for(int i = 1; i <= N; i++ ) //完全二叉树根结点存储在1号结点scanf("%d", &seq[i]);//数值递增排序sort(seq + 1, seq + N + 1);//中序遍历赋值in_order(1);//层序输出(直接顺序输出即可)for(int i = 1; i < N; i++)printf("%d ", cbt[i]);printf("%d\n---", cbt[N]);}return 0;
}

参考《算法笔记》

思路二

1、对输入值进行递增排序;
2、由结点数N计算树高、最底层的结点数,进而找到根节点,划分左右子树;
3、递归操作就行。

/**********************************
*@ID: 3stone
*@ACM: PAT.A1064 Complete Binary Serach Tree
*@Time: 18/8/13
*@IDE: VSCode 2018 + clang++
***********************************/
#include<cstdio>
#include<algorithm>
#include<queue>
#include<cmath>
#include<cstring>using namespace std;const int maxn = 1010;struct node{ //数结点int data; node* lchild;node* rchild;
};bool flag; //在建树过程中 标记是否能成功int seq[maxn]; //保存输入的前序序列
int N, cur_num; //结点数,输出时记录输出了多少node* new_node(int data_x) {node* root = new node;root->data = data_x;root->lchild = root->rchild = NULL;return root;
}//计算结点数为n的完全二叉树高度-1
int get_height(int n) {return (int)(log(n) / log(2)); //换底公式实现
}int get_root_key(int left, int right) {if(left == right) return left;int num_lchild;int height = get_height(right - left + 1); //返回树高减1int num_without_last_level = (int)pow(2.0, (double)height) - 1;  //k层完全二叉树的结点数 = 2^k - 1int num_last_level = right - left + 1 - num_without_last_level;  //二叉树最后一层的结点数 = 2^(k-1)int half_last_level = (int)((num_without_last_level + 1) / 2); if(num_last_level >= half_last_level){ //最后一层填了多于一半num_lchild = half_last_level * 2 - 1;} else {num_lchild = half_last_level + num_last_level - 1;}return left + num_lchild; //返回根节点编号
}//建立正常BST
node* create(int left, int right) {if(left > right) return NULL; //递归基//寻找根节点int root_key = get_root_key(left, right);node* root = new_node(seq[root_key]);//递归 建树root->lchild = create(left, root_key - 1);root->rchild = create(root_key + 1, right);return root;
}void BFS_traversal(node* root) {queue<node*> Q;Q.push(root);int num = 0;while(!Q.empty()) {node* front = Q.front();Q.pop();num++;if(num == N)printf("%d\n", front->data);elseprintf("%d ", front->data); if(front->lchild) Q.push(front->lchild);if(front->rchild) Q.push(front->rchild);}}int main() {while(scanf("%d", &N) != EOF) {for(int i = 1; i <= N; i++){scanf("%d", &seq[i]);}//递增排序sort(seq + 1, seq + N + 1);node* root = create(1, N);BFS_traversal(root);}return 0;
}

【ACM】- PAT. A1064 Complete Binary Serach Tree 【BST】相关推荐

  1. PAT甲级1064 Complete Binary Search Tree (30分):[C++题解]完全二叉搜索树BST

    文章目录 题目分析 题目链接 题目分析 思路: 第一步,构造含有n个结点的完全二叉树:第二步,将n个数值填入,使其满足二叉搜索树的性质. 对于第一步: 完全二叉树用一维数组可以存下,不过从根结点的下标 ...

  2. PAT A1110 Complete Binary Tree ——雨打梨花深闭门

    PAT A1110 Complete Binary Tree 思路不好,平添烦恼 判断是不是完全二叉树,一开始想的是这种树前n-1层是满的,最后一层可能有空位,所以遍历一下记录每层的数量,前几层根据数 ...

  3. C++学习之路 | PTA(甲级)—— 1064 Complete Binary Search Tree (30分)(带注释)(精简)

    1064 Complete Binary Search Tree (30分) A Binary Search Tree (BST) is recursively defined as a binary ...

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

    title: 04-树6 Complete Binary Search Tree(30 分) date: 2017-11-12 14:20:46 tags: - 完全二叉树 - 二叉搜索树 categ ...

  5. (浙江大学数据结构)PTA Complete Binary Search Tree (10 分)

    题目: A Binary Search Tree (BST) is recursively defined as a binary tree which has the following prope ...

  6. [浙大数据结构] 04-树6 Complete Binary Search Tree (30分)

    1 题目描述 A Binary Search Tree (BST) is recursively defined as a binary tree which has the following pr ...

  7. 【PAT】A1099. Build A Binary Search Tree (30)

    Author: CHEN, Yue Organization: 浙江大学 Time Limit: 200 ms Memory Limit: 64 MB Code Size Limit: 16 KB A ...

  8. 【PAT (Advanced Level) Practice】1099 Build A Binary Search Tree (30 分)

    深搜+广搜 #include <iostream> #include <stack> #include <queue> #include <vector> ...

  9. 1064 Complete Binary Search Tree (30 分)【难度: 一般 / 知识点: 完全二叉搜索树】

    https://pintia.cn/problem-sets/994805342720868352/problems/994805407749357568 二叉搜索数的中序遍历是有序的.故先将权值排序 ...

  10. 【数据结构笔记27】树习题:完全二叉搜索树(Complete Binary Search Tree)

    本次笔记内容: 树习题-CBST. 1 数据结构的选择 树习题-CBST. 2 核心算法 树习题-CBST. 3 计算左子树的规模 文章目录 题意理解 分析:用链表还是数组表示树 核心算法 核心递归算 ...

最新文章

  1. SoftReference
  2. SCCM 2012系列1 服务器准备上
  3. python的沙盒环境virtualenv(一)--作用
  4. idea maven中的profiles是干什么的
  5. 用window.location.href实现刷新另个框架页面
  6. 腾讯云低延时直播系统架构设计与弱网优化实践
  7. 调光设备术语:调光曲线(转)
  8. iOS UIWebView URL拦截
  9. Android kernel Crash后,定位出错点的方法
  10. 牛客网--19校招--获得最多的奖金
  11. ​马卡龙配色你好夏天PPT模板​
  12. 2:0!Dota2世界冠军OG被OpenAI碾压,全程人类只推掉两座外塔
  13. Unit Testing with JUnit - Tutorial
  14. 欧姆龙cp1h指令讲解_欧姆龙cp1h常用指令学习(十二)块程序
  15. 奇怪的 Win10 输入法问题
  16. [解决]Windows 8 丢失 MSVCR71.dll 或 MSVCP71.dll
  17. 51nod 1128 正整数分组 V2(二分数组)
  18. [车联网安全自学篇] ATTACK安全之从ATTCK看车联网安全如何落地企业SOC之基础知识篇
  19. 电厂GPS北斗时钟同步(卫星时间同步系统)组成及配置
  20. stm32h7 串口idle_【STM32H7教程】第30章 STM32H7的USART应用之八个串口FIFO实现

热门文章

  1. 磁珠 符号_关于PCB原理图中的FB-FB是磁珠的符号-电子元器件-电路图
  2. mysql合并两个表_MYSQL如何合并两个表
  3. java利用PageHelper.startPage(page, pageSize)分页
  4. 作为一只爬虫,如何科学有效地处理短信验证码?
  5. 自然语言分析包NLTK安装及入门
  6. MSP430开发环境配置
  7. 数学分析高等代数考研试题不断更新中
  8. 天线设计相关性能参数
  9. miix4linux双系统,联想Miix4 Pro一键重装系统win10教程
  10. win7如何显示文件扩展名?