https://pintia.cn/problem-sets/994805046380707840/problems/994805049870368768

将一系列给定数字顺序插入一个初始为空的二叉搜索树(定义为左子树键值大,右子树键值小),你需要判断最后的树是否一棵完全二叉树,并且给出其层序遍历的结果。

输入格式:

输入第一行给出一个不超过20的正整数N;第二行给出N个互不相同的正整数,其间以空格分隔。

输出格式:

将输入的N个正整数顺序插入一个初始为空的二叉搜索树。在第一行中输出结果树的层序遍历结果,数字间以1个空格分隔,行的首尾不得有多余空格。第二行输出YES,如果该树是完全二叉树;否则输出NO

输入样例1:

9
38 45 42 24 58 30 67 12 51

输出样例1:

38 45 24 58 42 30 12 67 51
YES

输入样例2:

8
38 24 12 45 58 67 42 51

输出样例2:

38 45 24 58 42 12 67 51
NO

代码:

#include <bits/stdc++.h>
using namespace std; const int maxn = 1e5 + 10;
int N;
int a[maxn];
vector<int> v(maxn);
vector<int> ans[maxn];
int depth = -1, cnt = -1; struct Node{
int val;
struct Node *left, *right;
}; int Pow(int a, int b) {
int ans = 1;
if(b == 0) return 1;
for(int i = 1; i <= b; i ++)
ans *= a; return ans;
} Node *BuildBST(Node *root, int x) {
if(!root) {
root = new Node();
root -> val = x;
root -> left = NULL;
root -> right = NULL;
} else if(x <= root -> val)
root -> right = BuildBST(root -> right, x);
else root -> left = BuildBST(root -> left, x); return root;
} void dfs(Node* root, int step, int index) {
if(!root) {
depth = max(depth, step + 1);
return;
} v[step] ++;
ans[step].push_back(root -> val);
dfs(root -> left, step + 1, index * 2);
dfs(root -> right, step + 1, index * 2 + 1); cnt = max(cnt, index);
} int height(Node* root) {
if(!root) return 0;
return max(height(root -> left), height(root -> right)) + 1;
} int main() {
scanf("%d", &N);
Node *root = NULL;
for(int i = 0; i < N; i ++) {
scanf("%d", &a[i]);
root = BuildBST(root, a[i]);
}
dfs(root, 0, 1);
bool flag = true;
for(int i = 0; i < depth - 2; i ++) {
if(v[i] != Pow(2, i)) {
flag = false;
break;
}
} for(int i = 0; i < depth; i ++) {
for(int j = 0; j < ans[i].size(); j ++) {
if(i == 0 && j == 0) printf("");
else printf(" ");
printf("%d", ans[i][j]);
}
}
printf("\n");
if(height(root -> left) - height(root -> right) > 1) flag = false;
if(cnt == N) printf("YES");
else printf("NO");
return 0;
}

  还有一种建树一会写吧 

PAT L3-010 是否完全二叉搜索树的更多相关文章

  1. (PAT)L2-004 这是二叉搜索树吗?(数据结构)

    题目链接:https://www.patest.cn/contests/gplt/L2-004 一棵二叉搜索树可被递归地定义为具有下列性质的二叉树:对于任一结点, 其左子树中所有结点的键值小于该结点的 ...

  2. PAT 天梯赛 是否完全二叉搜索树   (30分)(二叉搜索树 数组)

    将一系列给定数字顺序插入一个初始为空的二叉搜索树(定义为左子树键值大,右子树键值小),你需要判断最后的树是否一棵完全二叉树,并且给出其层序遍历的结果. 输入格式: 输入第一行给出一个不超过20的正整数 ...

  3. PAT天梯赛练习题 L3-010. 是否完全二叉搜索树(完全二叉树的判断)

    L3-010. 是否完全二叉搜索树 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 将一系列给定数字顺序插入一个初始为空的二叉搜 ...

  4. PAT树_层序遍历叶节点、中序建树后序输出、AVL树的根、二叉树路径存在性判定、奇妙的完全二叉搜索树、最小堆路径、文件路由

    03-树1. List Leaves (25) Given a tree, you are supposed to list all the leaves in the order of top do ...

  5. PAT (天梯)L2-004. 这是二叉搜索树吗?

    L2-004. 这是二叉搜索树吗? 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 一棵二叉搜索树可被递归地定义为具有下列性质的 ...

  6. PAT 天梯赛 L2-004 这是二叉搜索树吗?

    递归判断+建树 题目链接:https://www.patest.cn/contests/gplt/L2-004 题解 二叉搜索树的特点就是其根节点的值是位于左右子树之间的,即大于左子树的所有值,但是小 ...

  7. PAT L3-016 二叉搜索树的结构

    https://pintia.cn/problem-sets/994805046380707840/problems/994805047903240192 二叉搜索树或者是一棵空树,或者是具有下列性质 ...

  8. PAT A1115 Counting Nodes in a BST (30 分)——二叉搜索树,层序遍历或者dfs

    A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie ...

  9. PAT A1143 Lowest Common Ancestor (30 分)——二叉搜索树,lca

    The lowest common ancestor (LCA) of two nodes U and V in a tree is the deepest node that has both U ...

随机推荐

  1. Docker 从入门到实践(二)Docker 三个基本概念

    一.Docker 的三个进本概念? 了解 Docker 的三个基本概念,就可以大致了解 Docker 的生命周期. 镜像(Image) 容器(Container) 仓库(Repository) 二.镜 ...

  2. winfrom之datagridview分页显示

    这次datagridview绑定数据并分页操作,因为用到了webservice,所以代码会详细讲解.QueryByCondition是一个查询函数 客户端: PageData pageData=new ...

  3. 大数据计算平台Spark内核全面解读

    1.Spark介绍 Spark是起源于美国加州大学伯克利分校AMPLab的大数据计算平台,在2010年开源,目前是Apache软件基金会的顶级项目.随着Spark在大数据计算领域的暂露头角,越来越多的 ...

  4. Loj#6183. 看无可看

    Loj#6183. 看无可看 题目描述 首先用特征根求出通项公式\(A_n=p\cdot 3^n+q\cdot(-1)^n\).通过给定的\(f_0,f_1\)可以解出\(p,q\). 然后我们要求的 ...

  5. nginx学习笔记(一)

    select模型主要是apache用   FD 文件描述符   soa架构 安装nginx ping baidu.com netstat -lntup 查看端口 cat /etc/redhat-rel ...

  6. mysql执行顺序

    SELECT语句执行顺序 SELECT语句中子句的执行顺序与SELECT语句中子句的输入顺序是不一样的,所以并不是从SELECT子句开始执行的,而是按照下面的顺序执行: 开始->FROM子句-& ...

  7. 【vue】vue +element 搭建项目,使用el-date-picker组件遇到的坑

    1.html <el-form-item prop="dateTime"> <el-date-picker v-model="messageDataFo ...

  8. GIF 生成软件

    Screen to Gif

  9. Luogu5221 Product

    Luogu5221 Product 求 \(\displaystyle\prod_{i=1}^n\prod_{j=1}^n{\frac{\operatorname{lcm}(i,\ j)}{\gcd( ...

  10. OpenCV3计算机视觉Python语言实现笔记(四)

    1. Canny边缘检测 OpenCV提供了Canny函数来识别边缘.Canny边缘检测算法有5个步骤:使用高斯滤波器对图像进行去噪.计算梯度.在边缘上使用非最大抑制(NMS).在检测到的边缘上使用双 ...