题目链接

https://www.patest.cn/contests/gplt/L3-010

思路

因为是 完全二叉搜索树

可以用 数据 建树的方式 然后 遍历一遍这个 数字 就是 层序遍历

遍历的过程中 需要判断一个 其中间的位置 是否有一个位置 是没有结点的

如果有 就不是 完全二叉搜索树

要注意 这个树的定义是 左子树键值大 右子树 键值小

AC代码

#include <cstdio>
#include <cstring>
#include <ctype.h>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <map>
#include <stack>
#include <set>
#include <numeric>
#include <sstream>
#include <iomanip>
#include <limits> #define CLR(a) memset(a, 0, sizeof(a))
#define pb push_back using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair<string, int> psi;
typedef pair<string, string> pss; const double PI = 3.14159265358979323846264338327;
const double E = exp(1);
const double eps = 1e-30; const int INF = 0x3f3f3f3f;
const int maxn = 1e3 + 5;
const int MOD = 1e9 + 7; int arr[maxn]; int n, l; int flag; vector <int> ans; void Build()
{
string temp = "";
CLR(arr);
int num;
scanf("%d", &arr[1]);
int len = 1;
for (int i = 1; i < n; i++)
{
scanf("%d", &num);
for (int j = 1; ; )
{
if (arr[j] != 0)
{
if (num < arr[j])
j = j * 2 + 1;
else
j *= 2;
}
else
{
arr[j] = num;
if (j > len)
len = j;
break;
}
}
}
for (int i = 1; i <= len; i++)
{
if (arr[i])
ans.pb(arr[i]);
else
flag = 0;
}
} int main()
{
scanf("%d", &n);
flag = 1;
Build();
vector <int>::iterator it;
for (it = ans.begin(); it != ans.end(); it++)
{
if (it != ans.begin())
printf(" ");
printf("%d", (*it));
}
printf("\n");
if (flag)
printf("YES\n");
else
printf("NO\n");
}

最新文章

  1. JAVA实现 springMVC方式的微信接入、实现消息自动回复
  2. node静态资源管理变迁之路
  3. partition by
  4. PDF模板报表导出(Java+Acrobat+itext)
  5. Java语言基础(七)
  6. C语言数组内存初始化
  7. 接口测试:如何定位BUG的产生原因
  8. mahout 查看kmeans结果的命令
  9. 开发中关于IPv6的问题
  10. XML实体解析器的作用
  11. css选择器以及使用场景
  12. (转)EVMON_FORMAT_UE_TO_TABLES procedure - move an XML document to relational tables
  13. JavaScript主流框架3月趋势总结
  14. Codeforces 594D REQ 线段树
  15. 关于 systemctl --user status 报错的问题
  16. InnoDB行记录格式(compact)、InnoDB数据页结构
  17. Stealth潜行风格游戏源码(Unity5x)
  18. Django查询 – id vs pk
  19. python -wordcloudan云词安装
  20. Vue04——vue自定义事件、Router、Vue-cli、发布上线

热门文章

  1. 使用iframe实现提交表单不刷新页面
  2. 语音按钮功能之UIButton的UIControlEventTouchUpInside没有执行问题
  3. 微信小程序 之三元运算符代替wx:if 来解决背景图片显示隐藏
  4. vue生命周期回调方法
  5. Codeforces 246E Blood Cousins Return(树上启发式合并)
  6. 2017 [六省联考] T6 寿司餐厅
  7. java三角形和菱形的打印
  8. Maven教程:tutorialspoint-maven
  9. Android传统View动画与Property动画基础及比较
  10. minimum-moves-to-equal-array-elements-ii(好)