PAT 天梯赛 L3-010. 是否完全二叉搜索树 【Tree】
题目链接
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");
}
PAT 天梯赛 L3-010. 是否完全二叉搜索树 【Tree】的更多相关文章
- PAT天梯赛练习题 L3-010. 是否完全二叉搜索树(完全二叉树的判断)
L3-010. 是否完全二叉搜索树 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 将一系列给定数字顺序插入一个初始为空的二叉搜 ...
- 天梯赛练习 L3-010 是否完全二叉搜索树 (30分) 数组建树模拟
题目分析: 本题的要求是将n个数依次插入一个空的二叉搜索树(左大右小,且没有重复数字),最后需要输出其层次遍历以及判断是否是完全二叉搜索树,通过观察我们发现, 如果这个树是用数组建立的,那么最后输出的 ...
- 二叉搜索树TREE(线段树,区间DP)
前言 线段树+区间DP题,线段树却不是优化DP的,是不是很意外? 题面 二叉搜索树是一种二叉树,每个节点都有一个权值,并且一个点的权值比其左子树里的点权值都大,比起右子树里的点权值都小. 一种朴素的向 ...
- PAT 天梯赛 L1-017. 到底有多二 【水】
题目链接 https://www.patest.cn/contests/gplt/L1-017 AC代码 #include <iostream> #include <cstdio&g ...
- PAT 天梯赛 L2-004 这是二叉搜索树吗?
递归判断+建树 题目链接:https://www.patest.cn/contests/gplt/L2-004 题解 二叉搜索树的特点就是其根节点的值是位于左右子树之间的,即大于左子树的所有值,但是小 ...
- PAT天梯赛L2-004 这是二叉搜索树吗【递归】
L2-004. 这是二叉搜索树吗? 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 一棵二叉搜索树可被递归地定义为具有下列性质的 ...
- PAT 天梯赛 是否完全二叉搜索树 (30分)(二叉搜索树 数组)
将一系列给定数字顺序插入一个初始为空的二叉搜索树(定义为左子树键值大,右子树键值小),你需要判断最后的树是否一棵完全二叉树,并且给出其层序遍历的结果. 输入格式: 输入第一行给出一个不超过20的正整数 ...
- PAT 天梯赛 是否同一棵二叉搜索树 (25分)(二叉搜索树 指针)
给定一个插入序列就可以唯一确定一棵二叉搜索树.然而,一棵给定的二叉搜索树却可以由多种不同的插入序列得到.例如分别按照序列{2, 1, 3}和{2, 3, 1}插入初始为空的二叉搜索树,都得到一样的结果 ...
- pat 团体天梯赛 L3-010. 是否完全二叉搜索树
L3-010. 是否完全二叉搜索树 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 将一系列给定数字顺序插入一个初始为空的二叉搜 ...
- pat 团体天梯赛 L2-004. 这是二叉搜索树吗?
L2-004. 这是二叉搜索树吗? 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 一棵二叉搜索树可被递归地定义为具有下列性质的 ...
随机推荐
- cin和scanf的速度差别
好长时间没有遇到这种问题了,以前虽然知道scanf比cin快,但是没想到快这么多,见图. 50万的数据. scanf输入: cin输入: 网上说用std::ios::sync_with_stdio(f ...
- vs-插件+配置
{ "window.zoomLevel": 0, "files.autoSave": "off", "editor.fontSiz ...
- luogu P1040 加分二叉树
题目描述 设一个n个节点的二叉树tree的中序遍历为(1,2,3,-,n),其中数字1,2,3,-,n为节点编号.每个节点都有一个分数(均为正整数),记第i个节点的分数为di,tree及它的每个子树都 ...
- 访问权限修饰符Protected专题
上图描述:A类在a包下,m()方法被protected修饰 上图描述:B类也在a包下,B类是A类的子类. 解析:B类和A类是同包类,B类是A类的子类,因此b对象可以调用m()方法. 上图描述:C类也在 ...
- javascript好文---深入理解定位父级offsetParent及偏移大小
前面的话 偏移量(offset dimension)是javascript中的一个重要的概念.涉及到偏移量的主要是offsetLeft.offsetTop.offsetHeight.offsetWid ...
- [转] 32位 PL/SQL Develope r如何连接64位的Oracle 图解
原文地址:LINK 由于硬件技术的不断更新,Win7系统逐渐成为主流,而且计算机内存逐渐增大,为了充 分的利用内存资源(因为32为系统最多只能用到3G左右的内存),提高系统性能,很多人开始使用Win7 ...
- indy9在程序关闭时出现terminate thread timeout的BUG解决办法
indy9在程序关闭时出现terminate thread timeout的BUG解决办法 INDY9线程有BUG,在退出程序的时候会报错:terminate thread timeout(终止线程超 ...
- 【转载】React入门实例教程-读书笔记
参考了这篇文章: http://www.ruanyifeng.com/blog/2015/03/react.html 其中github 安装配置见上一篇文章(link) 一.HTML 模板 使用 Re ...
- ssh bitbucket github
$ ssh-keygen -t rsa -C "mac" $ vi ~/.ssh/config Host bb User git HostName bitbucket.org Id ...
- 蜗牛—Android基础之button监听器
XML文件中有一个textView 和 一个button. <LinearLayout xmlns:android="http://schemas.android.com/apk/re ...